0

来源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)

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

4

2 回答 2

2

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

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

于 2013-10-01T02:53:43.693 回答
1

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

clang .... main.cpp  MaxSumOfSubArray.cpp

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

于 2013-10-01T02:55:55.610 回答