Linux: Compile and link principle

Under Linux, when we use GCC to compile the Hello World program, we only need to use the simplest command (the source code is hello.c)

gcc hello.c
./a.out

In fact, the above process can be broken down into 4 steps, namely preprocessing, compilation, assembly and linking, as shown in the following figure:
Insert picture description here

Precompiled

In the pre-compilation stage, files such as .cpp and .h are compiled into an .i file. The pre-compilation process in the first step is equivalent to the following command:

gcc -E hello.c -o hello.i  #-E表示只进行预编译,-o指定生成文件名

The pre-compilation stage mainly processes the pre-compiled instructions starting with "#" in the source code files. For example, "#include", "#define", etc. The main processing rules are as follows:

  • Delete all #define and expand all macro definitions
  • Process all precompiled instructions, such as #if, #ifdef, #elif, #else, #endif
  • Process the #include precompiled directive and insert the included file into the position of the precompiled directive. (This process is carried out recursively, which means that the included file may also include other files)
  • Delete all the comments "//" and "/* */"
  • Add line number and file name identification, so that the compiler can generate line number information for debugging when compiling and can display the line number when compiling errors or warnings
  • Keep all #pragram compiler directives, because the compiler will use them

The pre-compiled .i file does not contain any macro definitions, because all the macros have been expanded and the included files are also inserted into the .i file. You can check whether the macro definition is correct or not by looking at the .i file. Is the header file included correctly

The pre-compilation stage processing can actually be summarized as the following three points :

  • Processing precompiled instructions
  • Delete comment
  • Macro replacement

The content of the hello.c file and the hello.i file generated by pre-compilation are as follows
Insert picture description here

Compile

The compilation process is to perform a series of lexical analysis, syntax analysis, semantic analysis and optimization on the preprocessed file to generate assembly code files. This process is the core part of program construction. The command:

gcc -S hello.i -o hello.s

The content of the hello.c file and the compiled hello.s file are as follows
Insert picture description here

compilation

The assembly process turns the assembly code into machine executable instructions

gcc -c hello.s -o hello.o

The processing of this process includes: translate into binary, generate each segment, generate symbol table
. The hello.o file generated by assembly is a binary file, and it will be garbled to view it with vim. Use the objdump command to view

objdump -h hello.o

Insert picture description here
Insert picture description here

link

The processing of the link stage includes: merging of various segments, symbol analysis, symbol relocation
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/huifaguangdemao/article/details/108273123