28

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?

6
  • @Neil, not as such. Note: I was able to reproduce this problem for numerous attempts at compiling until I hit upon a "solution". And, I expect the answers to be more theoretical, than concrete due to the problem not showing up again. However, I am trying to gain a better understanding of what might cause a symbol referenced in two separate compilation units to conflict when no conflict should exist. And what, if any, additional steps I can take to diagnose and mitigate the problem myself.
    – rcollyer
    May 24, 2011 at 20:18
  • 1
    I'd give this a 90% chance that you didn't, in fact, actually clean all the object files (maybe libtool or something was hiding them in a hidden directory?). Nevertheless, if you can't reproduce it, there's no way to get to the bottom of this.
    – bdonlan
    May 24, 2011 at 20:41
  • @bdonlan, interesting idea, and it would be rather annoying if libtool performed that sort of caching. Presumably, through either bjam clean or rm *.o *.a all of the objects and libraries should have been removed. If not then, then upon recompiling, they should have been overwritten. Also, bjam, by default, compiles in different directories when you change compilers, so any caching by the libtool, or others, would make this sort of separation pointless. Also, caching of this sort would, in my mind, make it difficult to create reliable builds, making it rather offensive.
    – rcollyer
    May 24, 2011 at 21:33
  • 1
    @bdonlan, as to your point about irreducibility, as a former tester, these types of problems were the most frustrating for both the tester and the developer, and they would often get rejected because of it. However, I think there is merit to the questions: how could I get into this situation despite the precautions taken? And once in this situation, what can be done to get out of it? While lacking a specific fix, these questions expose potential pitfalls in the build process/language that may not be clear to novice/intermediate programmers.
    – rcollyer
    May 24, 2011 at 21:41
  • 1
    @bdonlan, bjam appears to use rm -f, or the os's equivalent, and I'm using mercurial for my VCS which has purge.
    – rcollyer
    Jun 1, 2011 at 3:28

1 Answer 1

58

This is often the result of defining an object in a header file, rather than merely declaring it. Consider:

h.h :

#ifndef H_H_
#define H_H_
int i;
#endif

a.cpp :

#include "h.h"

b.cpp :

#include "h.h"
int main() {}

This will produce a duplicate symbol i. The solution is to declare the object in the header file: extern int i; and to define it in exactly one of the source-code files: int i;.

9
  • 3
    the symbol was a function declared inline in the header, but lacked the keyword inline which this suggests may be necessary. But, my question on your solution is why is int i in object file a.o exposed in such a way that it conflicts with the one found in b.o? As far as I understood it, that should not occur. What am I missing?
    – rcollyer
    May 24, 2011 at 21:22
  • 5
    1) Names declared at file scope have external linkage. 2) An object may be declared in multiple translation unites, but must be defined exactly once.
    – Robᵩ
    May 24, 2011 at 21:39
  • the more I think about this, the more this seems like it may very well have been the cause. Unless I can repeat it, though, I will remain baffled as to why it compiled one day, but not the next.
    – rcollyer
    Jun 1, 2011 at 3:29
  • 4
    @9000 - You can #include the same header in two files, if it does not have a line like int i; or int i = 17;. It must only have declarations, not definitions -- extern int i; is OK.
    – Robᵩ
    Mar 26, 2014 at 2:00
  • 1
    @rcollyer: gcc before (I think) version 9 assumed that i in a.o and b.o are the same. This changed for later versions. Sep 20, 2022 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.