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.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
.
问题是main.cpp
已经包括B.cpp
和A.cpp
。在你的构建过程中,你也编制B.cpp
并A.cpp
并试图链接B.o
和A.o
非常久远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.
链接B.o
和A.o
导致符号display
和square
被定义多次。display
定义了 3 次,square
定义了 2 次。
You just compile and build main.cpp
. Do not build A.cpp
and B.cpp
.
您只需编译和构建main.cpp
. 不要构建A.cpp
和B.cpp
。
Secondway is that make A.cpp
and B.cpp
to A.h
and B.h
and functions inline
. So, they will be compiled only once.
第二种方式是 make A.cpp
and B.cpp
toA.h
和B.h
and 函数inline
。所以,它们只会被编译一次。
Thirdway, do not include B.cpp
in 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 下)解决了这个问题。