A complete function declaration includes the return value type, call protocol name, function name, parameter information, and so on, to record all the prototype information of a function in a single string for easy identification and organization of the function, the VC compiler uses a technology called Name Decoration, the purpose is to orchestrate the original Name, call protocol, and return value information of a function into a new Name, called the Decorated Name, according to certain rules ). Let's take a look at the prototype of a function TestFunction and its modifier name.
Int TestFunction (HWND hWnd, int n)
Modifier name? TestFunction @ YAHPAUHWND __@ H @ Z
The modified Name no longer contains space and parentheses, which are not easy to store. It combines multiple parts into a coherent single whole. Therefore, Name modification is also called Name Mangling ).
The naming rules used by C compilers and C ++ compilers are different, which means that C and C ++ languages are supported at the same time for VC, the same function may have different modifier names. For example, for the void _ cdecl test (void) function, the modified name compiled according to the C Specification is _ test, and the modified name compiled according to the C ++ specification is? Test @ ZAXXZ. Because the connector uses the modifier name to connect to the target file, if the caller and implementation are different in the compilation specification, the following error occurs during connection:
Error LNK2001: unresolved external symbol "void _ cdecl Test (void )"(? Test @ YAXXZ)
By default, the VC compiler determines the source file type based on the source file extension and selects the compiler ,. c Represents the C file ,. cpp and. cxx indicates the C ++ file, and supports selecting the compiler using the compilation options/Tc,/Tp,/TC, And/TP options.
Specifically, "/TC" and "file name" indicate that the mandatory file is a C file, and "/TP" and "file name" indicate that the mandatory file is a C ++ file, use the/TC option to forcibly specify that all files to be compiled are c files. Use the/TP option to forcibly specify that all files to be compiled are c ++ files, in the C ++ file, use the extern "C" keyword to declare the name modification rules of C. Are the modified names of C ++ question marks? .
C language uses different call protocols to generate modifier names. When _ cdecl (C Call Protocol) is used, an underscore is added before the function name, regardless of parameters and return values. Use the _ fastcall function and add a @ symbol before and after the function name, followed by the parameter length. The return value is not considered. For example, the modifier name of extern "C" int _ fastcall test (int n) is @ test @ 4. for a function that uses the standard call protocol (_ stdcall), add an underscore before the function name, followed by the parameter length. The return value is not considered. For example, extern "C" int _ stdcall test (int n, int
M) the modifier name is _ test @ 8
C ++ naming rules are more complex than C. Class Name and namespace information must be considered. Different compilers use different rules, even for the same compiler, there may be differences between different versions. The following uses the Visual C ++ compiler as an example:
1. Question Mark prefix
2. constructor and destructor belong to special function names? 0 and? 1, new, delete, =, + and ++ are respectively named? 2 ,? 3 ,? 4 ,? H and? E
3. If the name is not a special function name, add a separator @
4. if it is a class method, the class name and parent class name are added in sequence from the class to which the class belongs. Each class name is followed by a @ symbol. After all class names are added, add the @ symbol and character Q or S (static method). If it is not a class method, add the @ symbol and character y directly.
5. call the Protocol Code. For a function that does not belong to any class, the code of the C call protocol (_ cdecl) is a ,__ fastcall, the code of the standard call protocol (_ stdcall) is G. For Class methods, a character a is added before the call protocol, and the code of this call protocol is E.
6. Return Value encoding. For example, character H indicates the return value of the integer type.
7. encode the parameter list, ending with the @ symbol
8. suffix Z.
In general
1. Are they all? Start, end with the character Z, separated by the @ symbol in the middle into multiple parts, and the length of the name is up to 2048 characters.
2. the basic structure of a class function is :? Method Name @ Class Name @ call protocol return type parameter list Z
3. The basic structure of a function that does not belong to any class is :? Function Name @ Y call return type parameter list Z