Tuesday 3 September 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...