DJG
DJG

Reputation: 495

Linker Command Failed, Duplicate Symbol

I have a simple project with three files. Main.cpp, CountTriangles.cpp, and CountTriangles.hpp. When I try to build/run, I get "Linker command failed with exit code 1" and, in the log, I find "ld: 1 duplicate symbol for architecture x86_64".

main.cpp:

#include "CountTriangles.cpp"

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

CountTriangles.cpp:

#include "CountTriangles.hpp"
using namespace std;

int TriangleCount::count(int N){
    int helper = 1;
    return helper;
}

CountTriangles.hpp:

#ifndef CountTriangles_hpp
#define CountTriangles_hpp

#include <iostream>
#include <stdio.h>

class TriangleCount{
    public:
        int count(int N);
};

#endif /* CountTriangles_hpp */

Upvotes: 0

Views: 423

Answers (1)

Cinder Biscuits
Cinder Biscuits

Reputation: 5241

In main.cpp you include #include "CountTriangles.cpp" but you should be in including the header CountTriangles.hpp

Since the definition of TriangleCount::count(int N) is then being compiled twice, redefined, you get the resulting duplicate symbol error.

Upvotes: 1

Related Questions