$ ls main.cpp myheader.cpp myheader.h $ g++ -c main.cpp $ g++ -c myheader.cpp $ g++ -o main main.o myheader.o myheader.o:(.data+0x0): multiple definition of `b' main.o:(.data+0x0): first defined here collect2: ld returned 1 exit status $ mv main.cpp main.c $ mv myheader.cpp myheader.c $ rm *.o $ gcc -c main.c $ gcc -c myheader.c $ gcc -o main main.o myheader.o myheader.o:(.rodata+0x0): multiple definition of `a' main.o:(.rodata+0x0): first defined here myheader.o:(.data+0x0): multiple definition of `b' main.o:(.data+0x0): first defined here collect2: ld returned 1 exit status In my project, I need to put the following statement in a header file: int lawCurrentEngine = NONE ; // NONE has been enum-med previously because I need to declare a function int lawCloseEngine ( int engineID = lawCurrentEngine ) ;I include the header file containing these two lines in two cpp files, and I get a multiple definition error for lawCurrentEngine just like in the given test case. If I push the lawCurrentEngine declaration to one of the cpp-s (it's not needed in the other cpp) I am unable to provide the default argument for the lawCloseEngine which can be done only in the header.
I don't understand how I am getting such an error when I have used the #ifndef #define #endif technique as per good programming practice.
Please help, Thanks. Shriramana Sharma.
Attachment:
multiple-declaration-problem.tar.gz
Description: application/gzip