C++ to Java conversion question about extern "C"
I have to convert some C/C++ code to Java. My C++ is extremely rusty.
In a .h
file, I have the following:
#ifdef __cplusplus
extern "C" {
#endif
/* tons of declarations */
#ifdef __cplusplus
} /* extern C */
#endif
What is the use of extern "C"
? What does it mean? Is it telling the compiler that corresponding code should be interpreted as pure C, rather than C++?
EDIT
Thanks for the answers so far. The history of the code I have to convert is that it seems like a part was written in C first, then the rest was written in C++. So my header file seems to correspond to 'old' C code.
I'll convert this code into a public final c开发者_开发知识库lass
with static method and attributes. No overriding.
It's telling the compiler that function signatures should be C compatible. Name mangling are different in C and C++ (i.e. C++ supports method overloading while C does not) so in order to make some function calls (i.e. some DLL dynamic calls) compatible with C signature, you use extern C
in C++ code.
Take a look at this StackOverflow question for a great explanation on the topic.
Extern "C"
is used to indicate a C-style linking instead of C++-style. The C++ compiler adds type information to the function symbol, but the C compiler does not.
If your library has to be used by an external application, extern "C"
is mandatory because C++ compilers are mostly incompatible regarding type information. This is the case when you have to create a native implementation in Java, for instance.
Extern "C" is indicating a C linking style instead of a C++ style. It tells the compiler "please do not mangle my function names". It is used so that a C++ program can be linked with a C program. If you are converting something to Java, it is probably safe to ignore this. This line would only be important if you were trying to convert code to C or C++.
精彩评论