简体   繁体   English

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

[英]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.

相关问题 C ++链接器错误(“链接器命令失败,退出代码为1”) - C++ Linker Error (“Linker command failed with exit code 1”) Xcode链接器命令失败,退出代码为1 c ++ - Xcode linker command failed with exit code 1 c++ Xcode C ++错误“链接器命令失败,退出代码为1” - Xcode C++ Error “linker command failed with exit code 1” 链接器命令失败,退出代码为1 C ++ Xcode - linker command failed with exit code 1 C++ Xcode C ++:Linker命令在xcode中的退出代码为-1 - C++:Linker command with exit code -1 in xcode 简单的c ++代码无法编译(链接器命令失败,退出代码为1) - simple c++ code does not compile (linker command failed with exit code 1) XCode C++ 链接器命令失败,退出代码为 1(使用 -v 查看调用) - XCode C++ linker command failed with exit code 1 (use -v to see invocation) 编译我的 C++ 程序:clang: error: linker command failed with exit code 1(使用 -v 查看调用) - Compiling my c++ program: clang: error: linker command failed with exit code 1 (use -v to see invocation) C++ 无法编译简单的类头(链接器命令失败,退出代码为 1) - C++ Can't compile simple class header (linker command failed with exit code 1) XCode C ++:链接器命令失败,未找到-lboost_regex的退出代码1库 - XCode C++: Linker command failed with exit code 1 library not found for -lboost_regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM