There are four steps for GCC to compile C source code: preprocessing ----> compiling ----> compilation ----> Link.
You can use GCC parameters to control the execution process, so that you can gain a deeper understanding of the process of compiling C Programs.
The following describes how to compile a program.
#include <stdio.h>int main(){ printf("happy new year!\n"); return 0;}
1: preprocessing:The compiler compiles the header file of the C program and replaces the Macro. You can refer to the gcc parameter-E.
Command: gcc-E hello. c-o hello. I
Purpose: Output hello. c preprocessing hello. I
At this stage, the compiler mainly performs lexical analysis, syntax analysis, semantic analysis, and so on. After checking that there are no errors, the code is translated into an assembly language. Refer to the gcc parameter-S.
The compiler (ccl) translates the text file hello. I into the text file hello. s, which contains an assembly language program. Each statement in an assembly language program is described in a standard text format.
A low-level machine language command.
Command: gcc-S hello. I-o hello. s
Purpose: Compile the pre-processing output file hello. I into a hello. s file.
3: Assembly:Convert the. s file generated in the compilation phase to the binary target code. Refer to gcc parameter-c. The assembler (as) translates hello. s into machine language commands, packs these commands into formats that can be relocated to the target program, and saves the results in the target file hello. o. The hello. o file is a binary file whose Byte encoding is the machine language.
Command: gcc-c hello. s-o hello. o
Purpose: Compile and output the Assembly output file hello. s to output the hello. o file.
4: link:Link the obj file to an executable file: The linker (ld) is responsible for merging the. o file. The result is the hello file, which is the target file of the course execution. It can be loaded into the memory and called by the system.
Command: gcc hello. o-o hello