Terry
Terry

Reputation: 299

C++ linker command failed with exit code 1

Source of MaxSumOfSubArray.cpp:

#include <iostream>
namespace MaxSumOfSubArray {

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

}

Source of 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?

Upvotes: 0

Views: 18927

Answers (2)

CS Pei
CS Pei

Reputation: 11047

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.

Upvotes: 1

John3136
John3136

Reputation: 29265

Looks like you include MaxSumOfSubArray.cpp in main.cpp AND try to compile it. Do one or the other, not both...

Hint: normally you wouldn't include a .cpp in another.cpp (you include .h files)

Upvotes: 2

Related Questions