这是来自 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/