Xcode C++ :: 架构 x86_64 的重复符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27436981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode C++ :: Duplicate Symbols for Architecture x86_64
提问by Ahmad
I am new to Xcode and when I build the following code (an MWE), I get the following error
我是 Xcode 的新手,当我构建以下代码(MWE)时,出现以下错误
ld: 3 duplicate symbols for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
ld:架构 x86_64 clang 的 3 个重复符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
I have three files as following;
我有以下三个文件;
main.cpp
主程序
#include "B.cpp"
int main() {
square(5);
return 0;
}
B.cpp
B.cpp
#include "A.cpp"
void square(int n){
display(n*n);
}
A.cpp
A.cpp
#include <iostream>
using namespace std;
void display(int num){
cout<<num;
}
I have tried different methods mentioned on stack overflow like change "Build Active Architecture Only" to "Yes" and some others but the error still persists.
我尝试了堆栈溢出中提到的不同方法,例如将“仅构建活动架构”更改为“是”和其他一些方法,但错误仍然存在。
回答by doptimusprime
Problem is that main.cpphas included B.cppand A.cpp. In your build process, you are also compiling B.cppand A.cppand trying to link B.oand A.oalongwith main.o.
问题是main.cpp已经包括B.cpp和A.cpp。在你的构建过程中,你也编制B.cpp并A.cpp并试图链接B.o和A.o非常久远main.o。
Linking B.oand A.ocauses symbols displayand squareto be defined multiple times. displayis defined 3 times and squaredefined 2 times.
链接B.o和A.o导致符号display和square被定义多次。display定义了 3 次,square定义了 2 次。
You just compile and build main.cpp. Do not build A.cppand B.cpp.
您只需编译和构建main.cpp. 不要构建A.cpp和B.cpp。
Secondway is that make A.cppand B.cppto A.hand B.hand functions inline. So, they will be compiled only once.
第二种方式是 make A.cppand B.cpptoA.h和B.hand 函数inline。所以,它们只会被编译一次。
Thirdway, do not include B.cppin main.cpp. Just put function declaration instead of inclusion.
第三种方式,不包括B.cpp在main.cpp. 只需放置函数声明而不是包含。
//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.
通常,函数声明放在头文件中。如果在多种情况下需要这样做,请制作一个头文件。
回答by Mitesh Khatri
For me, changing 'No Common Blocks' from Yes to No ( under Targets->Build Settings->Apple LLVM - Code Generation ) fixed the problem.
对我来说,将“ No Common Blocks”从 Yes 更改为 No(在 Targets->Build Settings->Apple LLVM - Code Generation 下)解决了这个问题。

