Xcode C ++ :: Duplicate Characters for x86_64 Architecture

I am new to Xcode and when I create the following code (MWE) I get the following error

ld: 3 repeated characters for x86_64 architecture clang: error: linker command did not work with exit code 1 (use -v to call the call)

I have three files:

main.cpp

#include "B.cpp" int main() { square(5); return 0; } 

B.cpp

 #include "A.cpp" void square(int n){ display(n*n); } 

a.cpp

 #include <iostream> using namespace std; void display(int num){ cout<<num; } 

I tried various methods mentioned in the stack overflow, such as changing "Only building an active architecture" to "Yes" and some others, but the error still persists.

+9
source share
2 answers

The problem is that main.cpp included B.cpp and A.cpp . During the build process, you also compile B.cpp and A.cpp and try to bundle Bo and Ao together with main.o

Binding Bo and Ao calls several characters for the display and square characters. display defined 3 times, and square is 2 times.

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

The second way is to make A.cpp and B.cpp before Ah and Bh and inline functions. Thus, they will be compiled only once.

Third , do not include B.cpp in main.cpp . Just put a function declaration instead of including it.

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

Typically, function declarations are placed in header files. If this is required in several cases, create a header file.

+16
source

For me, change "No common blocks" from Yes to No (in Targets-> Build Settings-> Apple LLVM - Code Generation) solved the problem.

enter image description here

0
source

Source: https://habr.com/ru/post/1208859/


All Articles