1. Concept Analysis
The linker requires a different symbolic name for each different function. In C
, there is only one function per name, so this is easy to do. To meet internal needs, the C
language compiler may
translate function names like
spiff into _spiff
. This approach is called C language linkage ( C language linkage )
In C++
, the same name may correspond to multiple functions, and these functions must be translated into different symbol names. Therefore, the C++
compiler performs name mangling, or name mangling
, to generate different symbol names for overloaded functions. For example, it is possible to convert spiff
(int) to _spoff_i
and
spiff
(double,
double
) to
_spiff_d_d
. This approach is called C++
language linkage ( C++ language linkage )
2. Usage scenarios
1.
Use
precompiled functions in
the C library in the C++ program
extern "C" void spiff(int);// use C protocol for name look-up
2. Use precompiled functions in the C++ library in the C++ program
Method 1: extern void spoff(int);// use C++ protocol for name look-up
points out by default
Method 2: extern "C++" void spaff(int);// use C++ protocol for name look-up to
explicitly point out