Compilation process: pre-compile, compile, assemble and link

Original Address: http: //blog.csdn.net/u014120684/article/details/46352167

File extension is precompiled ii 

gcc -E hello.c -o hello.i 
precompiled process mainly among the source code file to the beginning of the pre-compiled instructions #, such as #include files head is inserted into this position all the #define macro definition is expanded, further there is to remove all comments

I is to compile files compiled into assembly code files, assembler code extension is .s 
gcc -o -S hello.i hello.s

But now the pre-compiled version of gcc and compile two steps combined into a single step, 
gcc -S hello.c -o hello.s 
or compiler CCL: CCL hello.c 
c ++ compiler: cclplus hello.cpp

The compilation is the assembly language code files into machine language, assembly instructions and just the control of a machine language, a language translation. 
An assembler AS 
AS hello.s -o hello.o 
or gcc -c hello.o hello.s -o 
or precompiled most common, compiler, assembler triple: gcc -c hello.c -o hello.o or: gcc -c hello.c 
so it seems, finally resulting object files .o machine language which is, in fact, is in accordance with its own executable file storage format, just and true executable files on a slightly different structure.

Links: The program is divided into a plurality of modules when these modules last between how combined to form a single program is to solve the problem. How the combination between the modules can be attributed to how communication between modules, communication between the most common C / C ++ module is a static language, there are two ways, one is the function calls between modules (I can see note: the relationship between C ++ header files and the CPP), the other is variable between modules access. 
Access function needs to know the address of the target function, variable access also need to know the address of the target variable. In principle, the process of linking the work is nothing more than a reference to the functions and variables to be amended, so that the program can find the address of the function and variables. 
.C source code files compiled into object files .o, link object files and libraries together to form the final executable file. The most common library is the runtime library (runtime library). 
Link time, assuming that we have a global variable called var, he was the target file A which we in the target file inside B to access the global variables, such as the inside B : 
MOV1 $ 0x2a, var 
comparable to the C language inside var = 42 
when compiled object file B, var compiler does not know the destination address, so in a case where u compiler can not determine the address, these strips Mov target address of the instruction is set to 0, the target waits for the link in the documents a and B are linked again when its correction.

Dynamic Link: to solve the problem of wasted space and update difficulties, does not use static libraries and dynamic libraries, dynamic link is to be split into individual modules in accordance with the procedure relatively independent parts, the program runs in when they link together to form a complete the program, rather than statically linked program as all the modules are linked into a separate executable file. 
-fPIC -shared GCC 
-shared generate a shared object represents

比如一个静态库libtest.a和一个动态库libtest.so,在链接libtest.a和hello.o的时候,生成的hello可执行文件里面就包含了libtest.a。以后执行hello的时候就算删除了libtest.a也能执行出来。而在链接libtest.so和hello.o的时候,生成的hello可执行文件里面就没libtest.so,如果删除了libtest.so则不能执行。

Guess you like

Origin blog.csdn.net/alss1923/article/details/78971743