C ++链接器命令失败,退出代码为1

时间:2013-10-01 02:49:37

标签: c++ linker namespaces

MaxSumOfSubArray.cpp的来源:

#include <iostream>
namespace MaxSumOfSubArray {

    void run() {
        std::cout << "hey hey";
    }

}

main.cpp的来源:

#include "MaxSumOfSubArray.cpp"

int main(int argc, const char * argv[])
{
    MaxSumOfSubArray::run();
    return 0;
}

但我收到错误:

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)

为什么我会收到此错误以及如何解决?

2 个答案:

答案 0 :(得分:2)

看起来你在main.cpp中包含MaxSumOfSubArray.cpp并尝试编译它。做一个或另一个,而不是两个......

提示:通常你不会在another.cpp中包含.cpp(你包括.h文件)

答案 1 :(得分:1)

您正在尝试编译并在命令行中

clang .... main.cpp  MaxSumOfSubArray.cpp

这是错误的,因为你已经在main.cpp中包含了MaxSumOfSubArray.cpp,你不需要再次指定它。