6

extern "C"我在文件开头看到一些 C++ 代码,如下所示:

#ifdef __cplusplus 
extern "C" {} 
#endif

这是什么意思?它是如何工作的?

4

4 回答 4

5

它用于通知编译器禁用大括号内定义的函数的 C++ 名称修改。http://en.wikipedia.org/wiki/Name_mangling

于 2012-02-08T09:20:44.673 回答
4

可能不是这样,但更像是:

#ifdef __cplusplus 
extern "C" {
#endif

//some includes or declarations

#ifdef __cplusplus 
}
#endif

它告诉编译器对C指令中声明的任何内容使用名称修饰。

你现在拥有它的方式:

#ifdef __cplusplus 
extern "C" {} 
#endif

只是死代码。

于 2012-02-08T09:20:13.517 回答
0

它指定了一个链接规范
它告诉链接器如何链接代码。

当您想混合 C 和 C++ 代码时,它很有用。

于 2012-02-08T09:19:34.070 回答
0

Extern "C"- 通知编译器,指出的函数是以 C 风格编译的。

于 2012-02-08T09:20:41.047 回答