Analyze the following code:
/* = Sum. H = */
# Ifndef sum_h
# Define sum_h
# Include <stdio. h>
Int sum (int A, int B );
# Endif;
/* = Sum. c = */
# Include "sum. h"
Int sum (int A, int B)
{
Int c = A + B;
Return C;
}
/* = Main. cpp = */
# Include "sum. h"
Void Mian (){
Cout <sum (1, 2) <Endl;
}
The above three files are called and compiled successfully, but the following problems occur during execution:
OBJ: Error lnk2001: the external symbol "int _ cdecl sum (INT, INT)" that cannot be parsed )"(? Sum @ yahhh @ Z)
E:/programming/grapic/test/debug/test.exe: Fatal error lnk1120: 1 external command that cannot be parsed
Where is the problem? Sum. C is called in Main. cpp, that is to say, C is called in C ++. If no corresponding processing is done, a link error will occur.
Extern "C" indicates that the internal Symbol names generated by Compilation Use the c Convention. C ++ supports function overloading, but C does not. The compilation rules of the two are different. The name of the function in the symbol library after being compiled by C ++ is different from that in the C language. For example, assume that the prototype of a function is void Foo (int x, int y). After the function is compiled by the C compiler, its name in the symbol library may be _ Foo, the C ++ compiler generates names such as _ foo_int_int (different compilers may generate different names, but all adopt the same mechanism, the new name is called "mangled name "). A name such as _ foo_int_int contains the function name, number of function parameters, and type information. c ++ relies on this mechanism to implement function overloading.
So if C ++ code is called in C and how can C code be called in C ++?
Extern "C" indicates that the internal Symbol names generated by Compilation Use the c Convention.
1. How to call C in C ++?
C ++ calls C and extern "c". The function is to let the C ++ connector use the C method to find the symbol used to call the function.
The pen questions raised at the beginning of this article can be modified as follows:
/* = Sum. H = */
# Ifndef sum_h
# Define sum_h
# Include <stdio. h>
Int sum (int A, int B );
# Endif;
/* = Sum. c = */
# Include "sum. h"
Int sum (int A, int B)
{
Int c = A + B;
Return C;
}
/* = Main. cpp = */
Extern "C"
{
# Include "sum. h"
}
Void Mian (){
Cout <sum (1, 2) <Endl;
}
Execution successful
I believe this is almost clear.
2. How to call C ++ in C?
Reference C ++ functions in C (C calls C ++, the use of extern "C" tells the compiler to compile and encapsulate the functions defined by extern "C" in the CPP file in the way of C, of course, the C ++ syntax in the interface function is still compiled in C ++ mode)
Run:Test1.obj: Error lnk2019: the external symbol _ sum that cannot be parsed. This symbol is referenced in function _ main.
E:/programming/grapic/test/debug/test.exe: Fatal error lnk1120: 1 external command that cannot be parsed
/* ======= Sum. h ============= */
# Ifndef sum_h
# Define sum_h
# Include <stdio. h>
Int sum (int A, int B );
# Endif;
/* = Sum. cpp = */
# Include "sum. h"
Extern "C"
{
Int sum (int A, int B)
{
Int c = A + B;
Return C;
}
}
/* = Main. c = */
# Include "sum. h"
Void Mian (){
Cout <sum (1, 2) <Endl;
}
3. Standard and standard writing
Generally, we place the function declaration in the header file. When our function may be used by C or C ++, we cannot determine who calls it, this makes it uncertain whether to declare the function in extern "c". Therefore, we can add
# Ifdef _ cplusplus
Extern "C"
{
# Endif
// Function declaration
# Ifdef _ cplusplus
}
# Endif
The above statement form can be used comprehensively.
When C references functions and variables in C ++, C ++ functions or variables must be declared in extern "C, however, extern "C" cannot be used in C; otherwise, an error occurs during compilation. (Error: Error c2059: syntax error: 'string'. This error has been found online for a long time. The Chinese website has not found any direct explanation for the cause, the reason is that extern "C" is the keyword in C ++. If it is not C, all errors will occur.
/* = Sum. H = */
# Ifndef sum_h
# Define sum_h
# Include <stdio. h>
Int sum (int A, int B );
# Endif;
/* = Sum. cpp = */
# Include "sum. h"
Int sum (int A, int B)
{
Int c = A + B;
Return C;
}
/* = Main. c = */
# Include "sum. h"
Void Mian (){
Cout <sum (1, 2) <Endl;
}