【问题标题】:Linker Command Failed, Duplicate Symbol链接器命令失败,重复符号
【发布时间】:2017-10-17 19:49:21
【问题描述】:

我有一个包含三个文件的简单项目。 Main.cpp、CountTriangles.cpp 和 CountTriangles.hpp。当我尝试构建/运行时,我得到“Linker command failed with exit code 1”,并且在日志中,我发现“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 */

【问题讨论】:

  • #include source 文件,而不是头文件。不要那样做。
  • 在 main 中包含 #include "CountTriangles.cpp" 而不是 .hpp 文件。
  • 对于未来的问题,请阅读如何提出好的问题](stackoverflow.com/help/how-to-ask)。你真的应该复制粘贴错误输出。完整、完整,并附有可能存在的任何可能的信息说明。
  • 好的,我阅读了您引用的页面,因此将代码减少到最低限度。我对你所说的错误输出感到困惑。我认为我在删除特定于我的机器和文件结构的所有内容时包含了必要的内容。我还应该包括什么?

标签: c++ xcode linker


【解决方案1】:

main.cpp 中包含#include "CountTriangles.cpp",但您应该包含标题CountTriangles.hpp

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

【讨论】:

    猜你喜欢
    • 2014-05-20
    • 2012-08-20
    • 2017-05-30
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    相关资源
    最近更新 更多