C ++ compiler function compilation process and compiler function compilation process
In C ++, the type search process is relatively simple, basically the name search, which is not described here.
For a function (or member function) called in the. cpp file, the compiler mainly does the following three things:
1. Search for the name. first, you can find the name in the visible name entity in the compilation unit. (1) class member functions take precedence (class of the Object> base class ). stop searching once it is found. (2) If no, perform further search in the corresponding namespace. (3) If no, search by keoning in the namespace where the function parameter is located ).
2. Reload resolution. Select a function based on the name and the parameter matching principle.
3. The accessibility check is used to determine whether the selected function can be called.
Note:
1) according to the first article, it is clear that if the type wants to work with a non-member function, they should be placed in the same namespace. for example, the overload operators and parameter types of general types are placed in the same header file/or in the same namespace.
2) The function-specific template does not participate in the overload resolution. Therefore, if you want to use a function-specific template, the best way is to overload the function and use this feature in implementation.
3) The overload resolution takes place before the accessibility check. therefore, if the private function is unfortunately involved in the overload and selected, the compilation prompt cannot be accessed. this often implies ambiguity, and such a design itself is unreasonable. in other words, private parameters are not "private" in name search and reload ".
Take c. Twice (21) function call as an example:
A) Name Search: the compiler first looks for an entity scope (class, file, or namespace) that contains at least one Twice name, and puts the candidate function into the list of candidate entities. In this example, the compiler first searches for the class object where the object c is located and then stops finding it. If it does not, it searches for the base class and the peripheral namespace in sequence, until you find a scope that contains at least one candidate function. Note the following two points: 1) if an object is found, the search will be stopped, so not all functions with the same name will be considered; 2) The namespace of the parameter also belongs to the search range (keoning criterion ).
B) overload resolution: select the unique best match from the list of candidate overload functions. If it is not unique, it has two meanings. Note: 1) this is based on name search results; 2) the special template function is not involved in overloading.
C) Accessibility check: determines whether the selected function is accessible. This is the last step, later than the heavy load resolution.
The rules in this article are very important. After understanding them, many C ++ compilation problems will naturally be solved. Further articles will be introduced in the following sections: How to Determine the visible name (related to name search) in each compilation unit (CPP file) in C ++; how to find Keoning; and how to Overload) and override ).
How to use compiled functions in C ++
My previous study notes (found on the Internet): After reading them, you can definitely solve them.
15. Why should I add the extern "C" to call the function compiled by the C compiler in the C ++ program "?
First, as extern is a keyword in C/C ++ that indicates the range (visibility) of the function and global variable. This keyword tells the compiler, the declared functions and variables can be used in this module or other modules.
In general, the function and global variables referenced by this module to other modules are declared with the keyword extern in the module header file. 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.
Extern "C" is a linkage declaration. Variables and functions modified by extern "C" are compiled and connected in C language, 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
# I nclude "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 ...... remaining full text>
Keywords used by C ++ for programs compiled by C Compiler
What you are talking about is that c ++ contains c files.
The following is generally used to ensure that no suffixes are randomly added to the c ++ compiler and can be called according to the function name.
# Ifdef _ cplusplus
Extern "C "{
# Endif
// A piece of code
# Ifdef _ cplusplus
}
# Endif