A few days agoProgramIt is a C program written a long time ago. If you want to use the functions in it, the connection will find that the specific function cannot be found:
Assume that the old C library
C header file
/* ----------- C. H --------------*/# Ifndef _ C_H _ # DEFINE _ c_h_extern int add(Int x,Int y);# Endif
C source file
/* ----------- C. c --------------*/Int add(Int x,Int y){Return x+Y;}
C ++ calls
/* ----------- CPP. cpp --------------*/# Include "C. H" Void main(){Add(1,0);}
In this case, an error CPP. OBJ: Error lnk2001: unresolved external symbol "int _ cdecl add (INT, INT )"(? Add @ yahhh @ Z) because the target module of add cannot be found.
This reminds me of the C ++ overload function naming method and the C function naming method. Let's review: after the function is compiled in C, it is named "_" before the function name. For example, when the Add function is compiled into an OBJ file, it is actually named _ add, while C ++ is named differently, in order to implement function overloading, the same function name Add will be compiled into different names due to different parameters.
For example
Int add (INT, INT) =>Add @ yahhh @ Z,
Float add (float, float) ==>Add @ yammm @ Z,
The above is the naming method of vc6. Different compilers may be different. In short, the same function name with different parameters will be compiled into different target names, so that function overloading can call specific functions.
Compile CPP. cpp. The Compiler finds "add" in the CPP file.(1,0);And the function declaration is extern int add(Int x,Int y);The compiler decided to go to add @ yahhh @ Z. Unfortunately, he could not find it, because the C source file adds extern int(Int x,Int y);Compiled into _ add;
To solve this problem, C ++ adopts extern "C", which is our topic. To use the previous C library, you must learn it, you can see the following standard header files. You will find that many header files have the following structure:
# Ifndef _ H # DEFINE _ H # ifdef _ cplusplusextern "C"{# Endifextern int F1(Int,Int);Extern int F2(Int,Int);Extern int F3(Int,Int);# Ifdef _ cplusplus}# Endif/* _ H */
If we copy this header file, we can get
# Ifndef _ C_H _ # DEFINE _ C_H _ # ifdef _ cplusplusextern "C"{# Endifextern int add(Int,Int);# Ifdef _ cplusplus}# Endif/* _ C_H _*/
Compile in this way
/* ----------- C. c --------------*/
Int add(Int x,Int y){
Return x+Y;
}
The source file *. c ,__ cplusplus is not defined. extern "C"{}This does not take effect. For C, he only sees the extern int add.(Int,Int);
Compile the Add function into _ add (INT, INT );
Compile the C ++ source file
/* ----------- CPP. cpp --------------*/
# Include "C. H"
Void main()
{
Add(1,0);
}
In this case, the source file *. cpp ,__ cplusplus is defined. For C ++, what he sees is extern "C"{Extern int add(Int,Int);}The compiler will know the add(1,0); The called C-style function will know to find _ add (INT, INT) in C. OBJ insteadAdd @ yahhh @ Z;
This is why extern "C" is often seen in DLL"{},In Windows, C language is used. First, C can call these DLL correctly, and the user may use C ++ and extern "C"{} Will take effect