Let's start with HelloWorld...
#include <stdio.h> main( argc, *
Run the following command to generate an executable file from the source file:
Linux:
gcc -lstdc++ Hello.cpp -o Hello. g++ Hello.cpp -o Hello.
:
Windows:
cl Hello.cpp /link -:Hello.exe
: It is mainly used to replace the code text. (This replacement is a process .)
Linux:
cpp Hello.cpp >-E Hello.cpp -++ -E Hello.cpp -o Hello.i
Line number and file name identifier explanation:
# __u_long;
Do not generate line numbers and file name identifiers:
cpp -P Hello.cpp >-E -P Hello.cpp -++ -E -P Hello.cpp -o Hello.i
Windows:
cl /E Hello.cpp > Hello.i
Line number and file name identifier explanation:
283 "C:\\Program Files\\Microsoft Visual Studio\\VC98\\include\\stdio.h" __cdecl clearerr(FILE * __cdecl fclose(FILE * __cdecl _fcloseall();
Do not generate line numbers and file name identifiers:
cl /EP Hello.cpp > Hello.i
: Perform a series of pre-processed files(Lex ),(Yacc), Semantic AnalysisAndThe assembly code is generated later. This process is the core part of the program construction.
Linux:
/usr/lib/gcc/i586-suse-linux/../cc1 Hello.cpp
.file ,@progbits
For cpp files with c ++ features, use cc1plus for compilation, or use the gcc command for compilation (it will use the extension name to choose to call cc1plus or cc1plus)
/usr/lib/gcc/i586-suse-linux/./-S Hello.cpp -++ -S Hello.cpp -o Hello.s
Windows:
cl /FA Hello.cpp Hello.asm
The Hello. asm file generated by vc6 is as follows:
FLAT, FLAT, OFFSET esp,
: Assembly code-> machine commands.
Linux:
Hello.s --c Hello.cpp -++ -c Hello.cpp -o Hello.o
Windows:
cl /c Hello.cpp > Hello.obj
At this point, the generated target file is similar to the final executable file in structure.
: The link mentioned here should be called static link strictly. Multiple Target files, libraries-> final executable files (the splicing process ).
Executable File category:
Linux:
ld - /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/gcc/i586-suse-linux/./crtbeginT.o -L/usr/lib/gcc/i586-suse-linux/./ -L/usr/lib -L/lib Hello.o --start-group -lgcc -lgcc_eh -lc --end-group /usr/lib/gcc/i586-suse-linux/./crtend.o /usr/lib/crtn.o -o Hello.
Windows:
link /subsystem:console /:Hello.exe Hello.obj
Static LibraryEssentially, it is a compressed package containing a bunch of intermediate target files. Just like a zip file, the external symbol addresses contained in each intermediate file are not corrected by the linker.
Linux:
ar -t libc.a
Windows:
lib /list libcmt.lib
Linux :【]
ar -x /usr/lib/libc.a
Windows :【]
lib libcmt.lib /extract:build\intel\mt_obj\atof.obj
Linux:
ar -rf test.a main.o fun.o
Windows:
lib /:test.lib main.obj fun.obj
Each function or variable has its own unique name to avoid confusion between different variables and functions during the link process.
In the link, functions and variables are collectively referred to as symbols. function names or variable names are symbol names, and function or variable addresses are symbol values.
Each target file has a symbol table with the following symbols:
(1) Global symbols defined in the current target file, which can be referenced by other target files
For example, global variables and global functions
(2) The Global symbols referenced in this target file are not defined in this target file-External symbols)
Such as the extern variable, printf, and other library functions.
(3) segment name. This symbol is generated by the compiler and its value is the starting address of the segment.
Such as. text and. data of the target file.
(4) Local symbols, which are visible inside
For example, static variables
During the link process, we are more concerned with the preceding and.
Linux:
--t Hello.obj
You can install MinGW on windows to obtain these tools.
Windows:
dumpbin /symbols Hello.obj
Symbol modification is actually the process of renaming a variable or function. The factors that affect the name include:
(1) There are differences in modification rules for different languages
For example, the foo function is modified to _ foo in C and to _ foo _ in Fortran _
(2) features introduced by object-oriented languages (such as C ++)
Such as class, inheritance, virtual mechanism, overload, namespace, etc.
Function signatures are used to identify different functions, including function names, parameter types and numbers, classes and namespaces, call conventions, and other information.
The symbolic modification and function signature rules of Visual C ++ are not made public, but Microsoft providesYou can convert the modified name to a function prototype.
Use, force the C ++ compiler to use the C language rules for symbol Modification
g_nTest2 = add( a,
[Wiki]
For C/C ++, the default function of the compiler and the initialized global variables are strong symbols, and uninitialized global variables are weak symbols.
GCC can use "_ attribute _ (weak)" to define any strong symbol as a weak symbol.
__attribute__((weak)) ext; __attribute__((weak)) fun1(); fun2() __attribute__((weak)); strong = __attribute__((weak)) weak2 = ;
Above, weak1 and weak2 are weak symbols, and strong and main are strong symbols.
For the concept of strong and weak symbols, the linker processes and selects the global symbols that have been defined multiple times according to the following rules:
(1) The strong symbol cannot be defined multiple times. Otherwise, the linker reports an error of repeated symbol definitions.
(2) If a symbol is a strong symbol in a target file and is a weak symbol in other files, select a strong symbol.
(3) If a symbol is weak in all target files, select one of the most occupied space.
A symbolic reference to an external target file must be correctly determined when the target file is eventually linked to an executable file. If the definition of the symbol is not found, the compiler reports an error with the symbol as the definition, this is called a strong reference;
There is also a weak reference corresponding to it. When dealing with weak references, even if the symbol is not defined, the linker will not report an error. The default value is 0 or a special value.
GCC can declare an external function reference as a weak reference through "_ attribute _ (weakref.
__attribute__ ((weakref)) (NULL !=
Or the program can define a weak reference for some extension function modules. When we link the extension module with the program, the function module can be used normally;
If some functional modules are removed, the program can be properly linked, but the corresponding functions are missing, which makes the program functions easier to crop and combine.
#include <stdio.h><math.h> __attribute__((weak)) abs( abs( main( argc, * s = abs(()-, s);
For the linker, the entire Link process is to combine multiple input target files into one executable binary file.
Modern linker is basically usedMethod:
(1)
Scan all input target files, obtain the length, attributes, and locations of each segment, and collect all symbol definitions and symbol references in the symbol table in the input target file, unified into a global symbol table.
In this step, the linker will be able to get the segment lengths of all input target files, and combine them to calculate the length and position of each segment in the output file after merging, and establish a ing relationship.
(2)
Use all the information collected in the first step to read the data and Relocation information in the middle section of the input file (there is a Relocation Table ), in addition, it resolves and migrates symbols, and adjusts the addresses (external symbols) in the code.
Programmer's self-cultivation links, loading and libraries