In C ++ or CSource codeThe main reason is that the name adaptation method of each compiler is different. The following is from:
Http://blog.csdn.net/vinep/archive/2009/02/17/3899780.aspx an original article, paste it, ready to view.
Taking int func (INT, INT) as an example, the C compiler will adapt the name to _ FUNC, the C ++ compiler is adapted to _ func_int_int or _ funcii (different compilers ). If this function is compiled into a library in C, the function name in the target file is _ FUNC. When C ++ is called in this function, the C ++ compiler will find _ funcii in the target file. The result cannot be found and an error occurs. To prevent this problem, add an extern "C" before the function declaration to the C ++ compiler and do not modify the name, instead, go directly to _ FUNC. The C and C ++ compilers have different modifiers for function names. To ensure that functions written in C and C ++ can be called by each other, this
Link indicator
For functions compiled in C ++, if you want to use them in C, we usually add extern "c" to modify the function name according to the C style during compilation. Normally, functions written in C may be declared in the following form if they are used in other languages: # ifdef _ cplusplusextern "C" {# endif/***** some declaration or so *****/# ifdef _ cplusplus} # endif/* end __ cplusplus */That is, extern "C" has been added during the declaration without the need for calling. For example, Calling C library functions in VC ++ is not used as a special declaration.Usage of extern "C"
1. It can be a single statement
Extern "C" Double SQRT (double );
2. It can be a composite Statement, which is equivalent to adding extern "c" to all declarations in the composite statement"
Extern "C"
{
Double SQRT (double );
Int min (INT, INT );
}
3. the header file can be included, which is equivalent to adding extern "c" to all declarations in the header file"
Extern "C"
{
# I nclude <cmath>
}
4. You cannot add extern "c" to the function.
5. If a function has multiple declarations, you can add extern "c" to them, or you can only add them to the first declaration. The subsequent declaration will accept the rules of the first link indicator.
6. In addition to extern "C", there are extern "Fortran" and so on.