Xcode C++ :: Duplicate Symbols for Architecture x86_64

前端 未结 2 373
遇见更好的自我
遇见更好的自我 2021-01-01 20:42

I am new to Xcode and when I build the following code (an MWE), I get the following error

ld: 3 duplicate symbols for architecture x86_64 clang: err

相关标签:
2条回答
  • 2021-01-01 21:10

    Problem is that main.cpp has included B.cpp and A.cpp. In your build process, you are also compiling B.cpp and A.cpp and trying to link B.o and A.o alongwith main.o.

    Linking B.o and A.o causes symbols display and square to be defined multiple times. display is defined 3 times and square defined 2 times.

    You just compile and build main.cpp. Do not build A.cpp and B.cpp.

    Second way is that make A.cpp and B.cpp to A.h and B.h and functions inline. So, they will be compiled only once.

    Third way, do not include B.cpp in main.cpp. Just put function declaration instead of inclusion.

    //main.cpp
    
    void square(int);
    
    int main() {
      square(5);
      return 0;
    }
    

    Generally, function declarations are put in header files. If that is required in multiple cases, make a header file.

    0 讨论(0)
  • 2021-01-01 21:29

    For me, changing 'No Common Blocks' from Yes to No ( under Targets->Build Settings->Apple LLVM - Code Generation ) fixed the problem.

    enter image description here

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