【发布时间】:2017-07-28 00:29:05
【问题描述】:
我正在使用其他 C 语言包和一些 C++ 语言包编写代码。所以我的代码需要使用 C 例程和 C++ 类。我的计划是,使用extern "C" {} 方法将所有头文件包含在C 中,并使用g++ 进行编译。
因此,我将 C 中的所有页眉复制到一个目录并添加页眉和页脚,例如,
#ifdef __cplusplus
extern "C" {
#endif
//...
#ifdef __cplusplus
}
#endif
但是,它仍然无法编译。所以我制作了一个模拟 C 程序,以使其看起来清楚问题是如何出现的。
main.C
#include <stdio.h>
#include <A.h> //This is the problematic header file.
int main()
{
struct MMM m; //some struct in A.h
printf("How many times do I have to compile this? %d\n",1000);
return 0;
}
啊.h
#ifndef _A_H
#define _A_H
#ifndef ...
#define ... ...
#endif
#include <B.h>
#include <C.h>
#endif
它在编译模拟程序时给我的错误消息与在编译我正在处理的真实代码期间的错误消息相同。它是关于在 B.h 和 C.h 中定义的一些预处理器宏函数。但我想向您保证,所有这些头文件都写在 extern "C" {} 中。模拟程序仅用 C 语言编写,因此我能够使用 gcc 检查没有错误消息,并且效果很好。
所以我想知道的是,带有 extern "C" 的 g++ 不是和 gcc 一样工作吗?还是我错过了什么?有什么建议可以解决这个问题吗?
【问题讨论】:
-
如果不查看产生错误的代码就很难判断。 C 代码通常可以由 C++ 编译器按原样编译,但也有一些例外。