差值外部"C"表示"C". vs外部 [英] Difference extern"C" vs extern
问题描述
是对整个标头使用extern "C"
指定符,还是对每个函数都指定extern
是否存在区别?
Is there a difference whether I use the extern "C"
specifier for the entire header, or specify extern
for every function?
据我所知,没有,因为只有函数和变量可以在外部链接,所以当我在每个函数原型和extern变量之前使用extern
说明符时,我无需使用全局
As far as I know, there is none, since only functions and variables can be linked externally, so when I use the extern
specifier before every function prototype and extern variable, I have no need to use the global extern "C"
declaration!?
示例A:
#ifdef __cplusplus
extern "C" {
#endif
void whatever(void);
#endif
示例B:
extern void whatever(void);
推荐答案
C ++文件中extern "C"
的存在允许从C客户端调用程序代码调用特定的C ++函数.
The presence of extern "C"
in a C++ file allows to call the particular C++ function from a C client caller code.
有什么区别?
很久很久以前,C编译器仅通过名称生成代码并处理函数. 它没有考虑参数.
A long, long time ago C-compilers generated code and addressed functions by name only. It didn't consider parameters.
在C ++中引入重载函数时,需要extern "C"
为不同的函数指定相同的名称.例如void f()
和void f(int)
是C ++中的两个不同函数.
When overloaded functions were introduced in C++, extern "C"
was required to specify the same name for different functions. For example void f()
and void f(int)
are two different functions in C++.
C ++编译器通过名称处理来完成此任务.它将一些信息添加到与函数参数有关的函数名称中.
The C++ compiler accomplished this via name-mangling. It adds some info to the function name related to the functions parameters.
extern "C"
是对编译器的命令,用于引用较早的样式命名约定-无需弄乱".
extern "C"
is a command to the compiler to "refer to the older style naming convention - without mangling".
这篇关于差值外部"C"表示"C". vs外部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!