[英]C++ linker command failed with exit code 1
Source of MaxSumOfSubArray.cpp
: MaxSumOfSubArray.cpp
来源:
#include <iostream>
namespace MaxSumOfSubArray {
void run() {
std::cout << "hey hey";
}
}
Source of main.cpp
: main.cpp
来源:
#include "MaxSumOfSubArray.cpp"
int main(int argc, const char * argv[])
{
MaxSumOfSubArray::run();
return 0;
}
But I get the error: 但是我得到了错误:
duplicate symbol __ZN16MaxSumOfSubArray3runEv in:
/Users/li.tonghui/Library/Developer/Xcode/DerivedData/CppChallenges-eobfuxlkqjfgebendxkoqbsvsbmr/Build/Intermediates/CppChallenges.build/Debug/CppChallenges.build/Objects-normal/x86_64/main.o
/Users/li.tonghui/Library/Developer/Xcode/DerivedData/CppChallenges-eobfuxlkqjfgebendxkoqbsvsbmr/Build/Intermediates/CppChallenges.build/Debug/CppChallenges.build/Objects-normal/x86_64/MaxSumOfSubArray.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Why am I getting this error and how to solve it? 为什么会出现此错误以及如何解决?
Looks like you include MaxSumOfSubArray.cpp in main.cpp AND try to compile it. 看起来您在main.cpp中包含MaxSumOfSubArray.cpp并尝试对其进行编译。 Do one or the other, not both...
做一个或另一个,而不是两个...
Hint: normally you wouldn't include a .cpp in another.cpp (you include .h files) 提示:通常,您不会在另一个.cpp中包含.cpp(您包括.h文件)
you are trying to compile and in your command line 您正在尝试在命令行中进行编译
clang .... main.cpp MaxSumOfSubArray.cpp
which is wrong since you already includes MaxSumOfSubArray.cpp
in main.cpp, you dont need to specify it again. 这是错误的,因为您已经在main.cpp中包含
MaxSumOfSubArray.cpp
,因此无需再次指定它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.