I have a c++ program that compiled previously, but after mucking with the Jamfiles, the program no longer compiled and ld
emitted a duplicate symbol error
. This persisted after successively reverting to the original Jamfiles, running bjam clean
, removing the objects by hand, and switching from clang with the gcc front end to gcc 4.2.1 on MacOs 10.6.7.
A simplified description of the program is that there is main.cpp
and four files, a.h,cpp
and b.h,cpp
, which are compiled into a static library which is linked to main.o
. Both, main.cpp
and b.cpp
depend on the file containing the offending symbol, off.h
, through two different intermediate files, but neither a.h
nor a.cpp
depend in any way on off.h
.
Before you ask, I made sure that all files were wrapped in multiple definition guards (#ifndef
, #define
, #endif
), and while I did find a file that was missing them, it did not reference off.h
. More importantly, b.h
does not include anything that references off.h
, only the implementation, b.cpp
, makes any reference to off.h
. This alone had me puzzled.
To add to my confusion, I was able to remove the reference to off.h
from b.cpp
and, as expected, it recompiled successfully. However, when I added the reference back in, it also compiled successfully, and continued to do so after cleaning out the object files. I am still at a loss for why it was failing to compile, especially considering that the symbols should not have conflicted, I had prevented symbol duplication, and I had gotten rid of any prior/incomplete builds.
Since I was able to successfully compile my program, I doubt I'll be able to reproduce it to test out any suggestions. However, I am curious as to how this can happen, and if I run across this behavior in the future, what, if anything beyond what I've done, might I do to fix it?
See Question&Answers more detail:
os