【发布时间】:2012-02-29 17:15:28
【问题描述】:
我看到一些 C++ 代码在文件开头使用 extern "C",如下所示:
#ifdef __cplusplus
extern "C" {}
#endif
这是什么意思?它是如何工作的?
【问题讨论】:
-
这里有关于 extern "c" 的好信息:stackoverflow.com/questions/1041866/…
我看到一些 C++ 代码在文件开头使用 extern "C",如下所示:
#ifdef __cplusplus
extern "C" {}
#endif
这是什么意思?它是如何工作的?
【问题讨论】:
它用于通知编译器禁用大括号内定义的函数的 C++ 名称修改。 http://en.wikipedia.org/wiki/Name_mangling
【讨论】:
可能不是这样,但更像:
#ifdef __cplusplus
extern "C" {
#endif
//some includes or declarations
#ifdef __cplusplus
}
#endif
它告诉编译器使用 C 名称修饰来处理指令中声明的任何内容。
你现在的样子:
#ifdef __cplusplus
extern "C" {}
#endif
只是死代码。
【讨论】:
Extern "C" - 通知编译器,指出的函数是以 C 风格编译的。
【讨论】:
它指定了一个linkage specification。
它告诉链接器如何链接代码。
当你想mix C and C++ code时很有用。
【讨论】:
"C"是。