Understanding the origin of a linker duplicate symbol error

后端 未结 1 1873
耶瑟儿~
耶瑟儿~ 2020-12-10 01:30

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

相关标签:
1条回答
  • 2020-12-10 01:59

    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;.

    0 讨论(0)
提交回复
热议问题