2012-01-03 wcdj
Question:
What's the theory behind this: #ifdef __cplusplus extern "C" {I have been meeting the following in some c/c++ codes but I don't understand the theory behind it:
#ifdef __cplusplus
extern "C" {
#endif
I want to understand how it works. I know about the preprocessing but I don't know what that extern "C" in the code does. Someone teach me please!
Answers 1:
The C++ compiler does something called name mangling which facilitates function overloading.The extern "C" syntax tell the C++ compiler not to perform function overloading.
The code snippet that you posted first checks if the code being compiled is using a C++ compiler and if so starts a block in which name mangling will not be done.
Answer 2:
That typically encloses DLL's exported functions.You should know, C Language and C++ one use different approach

本文介绍了在C++中如何使用extern "C"来调用C的函数,以解决编译错误。通过示例代码,展示了不使用extern "C"声明时可能出现的问题,并提供了相关C++标准的引用和外部资源链接,帮助理解这一关键概念。
最低0.47元/天 解锁文章
724

被折叠的 条评论
为什么被折叠?



