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