c++ - 未创建目标文件

标签 c++ c gcc linker g++

这是来自 Richard Stevens 关于高级 Linux 编程的书。

所以当它教用 GCC、G++ 编译时, 我创建了一个名为 reciprocal 的文件夹,其中使用如下所示的代码创建了以下文件。

main.c:

#include <stdio.h>
#include "reciprocal.hpp"
int main (int argc, char **argv)
{
  int i;
  i = atoi (argv[1]);
  printf ("The reciprocal of %d is %g\n", i, reciprocal (i));
  return 0;
}

互惠.cpp:

#include <cassert>
#include "reciprocal.hpp"
double reciprocal (int i) {
  // I should be non-zero.
  assert (i != 0);
  return 1.0/i;
}

倒数.hpp:

#ifdef __cplusplus
extern "C" {
#endif
extern double reciprocal (int i);

#ifdef __cplusplus
}
#endif

这三个文件都在同一个文件夹中。现在我在终端 gcc -c main.c 中输入了命令并且创建了对象 main.o 但是当我写 g++ -c reciprocal.cpp它显示错误

reciprocal.cpp: In function ‘double reciprocal(int)’:
reciprocal.cpp:4:8: error: redefinition of ‘double reciprocal(int)’
reciprocal.cpp:4:8: error: ‘double reciprocal(int)’ previously defined here

这里出了什么问题?

最佳答案

您正在将其编译为 C++。将其编译为 C 或转储 extern "C"

关于c++ - 未创建目标文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22111349/

相关文章:

c - 内存和文件指针

gcc - 为 gcc/g++ 指定库的不同方式

c++ - 在 Visual Studio 2008 中使用 _DEBUG 预处理器定义 - C++

c - pcap 过滤器表达式

c++ - 将int转换为字符串并添加到c++中的现有字符串

c++ - 操作二维数组的可能优化

c - Eclipse - 在 printf() 调用后将 fflush(stdout) 作为默认值

c++ - 可以定义一个完全通用的 swap() 函数吗?

.net - OracleConnection 在托管 C++ 中抛出 EEMessageException

c++ - 获取标题栏文字大小 WINAPI