链接器命令失败,重复符号

时间:2017-10-17 11:39:31

标签: c++ xcode linker

我有一个包含三个文件的简单项目。 Main.cpp,CountTriangles.cpp和CountTriangles.hpp。当我尝试构建/运行时,我得到“链接器命令失败并退出代码1”,并在日志中找到“ld:1复制符号,用于架构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 */

1 个答案:

答案 0 :(得分:1)

main.cpp中,您加入了#include "CountTriangles.cpp",但您应该加入标题CountTriangles.hpp

由于TriangleCount::count(int N)的定义被编译两次,重新定义,因此会得到重复的符号错误。

相关问题