Tuesday, September 3, 2019

How does extern "C" work in C++?




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?


Answer



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.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...