C ++ language was originally created as "a better C ", however, this does not mean that the compilation and connection methods used by global variables and functions similar to C in C ++ are the same as those in C.
As a language to be compatible with C, C ++ retains the features of some procedural languages (known as "not completely oriented objects "), therefore, it can define global variables and functions that do not belong to any class. However, C ++ is an object-oriented programming language after all. To support function overloading, C ++ treats global functions differently from C.
Key points:
1. compatible with C (some procedural language features are retained) ==> global variables and functions that do not belong to any class can be defined.
2. c ++ in order to support function overloading in object-oriented languages, C ++ treats global functions differently from C.
I. In-depth revealing the double meanings of extern "C"
1. The function or variable specified by extern "C" is of the extern type;
Extern is a keyword in C/C ++ that indicates the range (visibility) of functions and global variables. This keyword tells the compiler, the declared functions and variables can be used in this module or other modules.
Statement extern int;
It is just a declaration of a variable. It does not define variable A and does not allocate memory space for variable. Variable A can only be defined once as a global variable in all modules. Otherwise, a connection error occurs.
In general, in the header file of the module (here the module can be temporarily understood as: a CPP file is a module), the functions and global variables referenced by this module to other modules are declared with the keyword extern. For example, if Module B wants to reference the global variables and functions defined in module A, it only needs to include the header file of module. In this way, when Module B calls a function in module A, although Module B cannot find the function in the compilation phase, no error is reported; it will find this function from the target Code Compiled by module A in the connection phase.
(The keyword corresponding to extern is static. The global variables and functions modified by it can only be used in this module. Therefore, a function or variable can only be used by this module and cannot be modified by extern "c .)
2. Variables and functions modified by extern "C" are compiled and connected in C language;
First, let's take a look at how C-like functions are compiled in C ++:
As an object-oriented language, C ++ supports function overloading, while Procedural Language C does not. 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 is _ foo, while the C ++ compiler generates names such as _ foo_int_int (different compilers may generate different names, but all adopt the same mechanism, and 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. For example, in C ++, the void foo (int x, int y) and void foo (int x, float y) functions generate different symbols, the latter is _ foo_int_float.
Similarly, variables in C ++ support both local variables and class member variables and global variables. The class member variables of the program written by the user may have the same name as the global variables, which are distinguished. In essence, the compiler uses a unique name for the variables in the class when compiling, similar to the function processing. This name is different from the global variable name with the same name in the user program.
Connection method when extern "C" is not added
Suppose in C ++, the header file of module A is as follows:
// Module A header file moduleA. h
# Ifndef MODULE_A_H
# Define MODULE_A_H
Int foo (int x, int y );
# Endif
Reference this function in Module B:
// Module B implements the file moduleB. cpp
# Include "moduleA. h"
Foo (2, 3 );
In fact, in the connection phase, the connector looks for symbols such as _ foo_int_int from the target file moduleA. obj generated by module!
Compilation and Connection Methods After the extern "C" clause is added
After the extern "C" statement is added, the header file of module A is changed:
// Module A header file moduleA. h
# Ifndef MODULE_A_H
# Define MODULE_A_H
Extern "C" int foo (int x, int y );
# Endif
In Module B's implementation file, foo (2, 3) is still called. The result is:
(1) When module A compiles and generates the foo target code, it does not perform special processing on its name and uses the C language;
(2) When the connector looks for the foo (2, 3) call for the target code of Module B, it looks for the unmodified symbol name _ foo.
If the function in module A declares that foo is of the extern "C" type, and Module B contains the extern int foo (int x, int y ), module B cannot find the function in module A, and vice versa.
Therefore, we can summarize the true purpose of the statement "extern" C "in one sentence: to implement mixed programming of C ++ and C and other languages.
(The birth of any syntactic feature in any language is not random and comes from the needs of the real world. When thinking about a problem, we should not just focus on how the language is made, but also ask why it is doing so and what the motivation is, so that we can better understand many problems)
Ii. Usage of extern "C"
1. Reference functions and variables in C language in C ++
When the C-Language header file (cExample. h) is included, the following processing is required:
Extern "C"
{
# Include "cexample. H"
}
In the header file of the C language, the external function can only be specified as the extern type. The C language does not support the extern "C" declaration. when the c file contains extern "C", a compilation syntax error occurs.
The source code of the three files in the example project of C ++ referenced by the author is as follows:
/* C header file: cexample. H */
# Ifndef c_example_h
# Define c_example_h
Extern int add (int x, int y );
# Endif
/* C language implementation file: cexample. C */
# Include "cexample. H"
Int add (int x, int y)
{
Return X + Y;
}
// C ++ implementation file, call Add: cppfile. cpp
Extern "C"
{
# Include "cexample. H"
}
Int main (INT argc, char * argv [])
{
Add (2, 3 );
Return 0;
}
If C ++ calls a. DLL written in C language, when it includes the header file of. DLL or the declared interface function, it should add extern "C ".
2. Reference functions and variables in C ++ in C.
The header file of C ++ needs to be added with extern "C", but the header file declared with extern "C" cannot be directly referenced in the C language, only the extern "C" function defined in C ++ should be declared as the extern type in the C file.
The source code of the three files contained in the example project of C ++ is as follows:
// C ++ header file cppExample. h
# Ifndef CPP_EXAMPLE_H
# Define CPP_EXAMPLE_H
Extern "C" int add (int x, int y );
# Endif
// C ++ implementation file cppExample. cpp
# Include "cppExample. h"
Int add (int x, int y)
{
Return x + y;
}
/* C implementation file cFile. c
/* Compilation errors: # include "cExample. h "*/
Extern int add (int x, int y );
Int main (int argc, char * argv [])
{
Add (2, 3 );
Return 0;
}
Add a static description.
Static has two meanings
(1) If the static int foo; clause is in the function, static indicates the storage attribute, indicating that foo is a static variable.
(2) If the static int foo; clause is located outside the function, foo is a global variable. static does not represent the storage property, but is used as a delimiter: it is used to restrict the visible range of the global variable foo and restrict its scope to the file where it is located. It is invisible to other files.