How does extern “C” work in C++? [duplicate]

旧城冷巷雨未停 提交于 2020-01-24 09:44:07

问题


I see some code in C++ using extern "C" at the beginning of the file like this:

#ifdef __cplusplus 
extern "C" {} 
#endif

What does this mean? How does it work?


回答1:


It's probably not like that, but more like:

#ifdef __cplusplus 
extern "C" {
#endif

//some includes or declarations

#ifdef __cplusplus 
}
#endif

It tells the compiler to use C name mangling for whatever is declared inside the directives.

The way you have it now:

#ifdef __cplusplus 
extern "C" {} 
#endif

is just dead code.




回答2:


It is used to inform the compiler to disable C++ name mangling for the functions defined within the braces. http://en.wikipedia.org/wiki/Name_mangling




回答3:


Extern "C" - notify the compiler,that the noted function is compiled in C style.




回答4:


It specifies a linkage specification.
It tells the linker how to link the code.

It is useful when you want to mix C and C++ code.



来源:https://stackoverflow.com/questions/9190455/how-does-extern-c-work-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!