【问题标题】:C/C++ duplicate symbols errorC/C++ 重复符号错误
【发布时间】:2012-05-03 19:05:21
【问题描述】:

我目前正在尝试编译一个混合了 cpp 和 c 的现有项目。

我的makefile如下;

execute: mgnsvd.o multifit.o cit.o  main.o
    g++ -Wall -g multifit.o cit.o main.o mgnsvd.o -lgsl -lgslcblas -o execute 

main.o: main.cpp cit.cpp
    g++ -Wall -g -c main.cpp

cit.o: cit.cpp mgnsvd.c multifit.h
   g++ -Wall -g -c cit.cpp

multifit.o: multifit.c mgnsvd.c multifit.h
   g++ -Wall -g -c multifit.c

mgnsvd.o: mgnsvd.c
    g++ -Wall -g -c mgnsvd.c

我的主要是一个相当简单的

// global libraries
#include <iostream>

// local files
#include "cit.cpp"

// using directives
using std::endl;
using std::cout;

// main
int main(){

  cout << "Hello, world" << endl; 

 return 0;
}

如果注释掉#include "cit.cpp" 它编译得很好。但是,如果我包含它,则会发生以下错误;

ld: duplicate symbol _first_function_in_cit in main.o and cit.o for architecture x86_64

_first_function 始终是第一个函数,并且没有在其他任何地方定义甚至声明/使用。目标文件的 grep 确认该函数似乎已合并到 main.o,但为什么呢?我以前没有从事过 C++/C 项目,所以也许我犯了一些规范错误?我也在 OSX 中,如果这有什么不同的话。

作为记录,没有类 - .h 文件包含一些结构和一些宏。

【问题讨论】:

  • 为什么要包含cit.cpp,我假设它是一个实现文件?你能给我们看看那个文件吗?可以一起编译文件。

标签: c++ c gcc compiler-errors g++


【解决方案1】:

cit.cpp 正在自行编译,并且还包含在 main.cpp 中,因此您将获得其中所有代码的两个副本。

【讨论】:

  • 我现在有点想删除这个问题。谢谢 - 所以习惯于在我的主要内容中包含至少一些内容(即标题)。
【解决方案2】:

不需要

 #include "cit.cpp"

cit.cpp 被编译为一个单独的单元,然后被链接。

通过上述包含,您将获得两次代码,结果是重复的符号

【讨论】:

    【解决方案3】:

    您正在编译 cit.cpp 以产生 cit.o,并且您正在使用 main.cpp 中的 #include "cit.cpp" attrocity 再次编译它。所以你当然会得到重复的符号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      相关资源
      最近更新 更多