I think I've managed to reproduce the problem with the following files, though I think I'm missing something very foolish here..!
makefile
Code:
main: test1.o main.o
g++ test1.o main.o -std=c++0x -g -o main
test1.o: test1.cpp test1.h
g++ test1.cpp -c -g -std=c++0x -o test1.o
main.o : main.cpp
g++ main.cpp -c -g -std=c++0x -o main.o
main.cpp
Code:
#include "test2.h"
#include "test1.h"
using namespace std;
int main()
{
std::list<int>::iterator lit;
lit = mylist.begin();
foo();
return 0;
}
test1.cpp
Code:
#include "test2.h"
void foo()
{
}
test1.h
Code:
#ifndef TEST1
#define TEST1
void foo();
#endif
test2.h
Code:
#ifndef TEST2
#define TEST2
#include<list>
std::list<int> mylist = {1,2,3,5,6,7,0};
#endif
The error I get is:
Originally Posted by
make
main.o: In function `main':
/home/manasij7479/test/main.cpp:5: multiple definition of `mylist'
test1.o:/home/manasij7479/test/test1.cpp:3: first defined here
collect2: ld returned 1 exit status
make: *** [main] Error 1