The process of compiling gcc into an executable file

Edit a test.c file

#include <stdio.h>

int main()
{
    
    
	printf("this is test program\n");
	return 0;
}

1. Pretreatment stage

At this stage, the compiler compiles the preprocessing part into the program. The "gcc -E" option means that only preprocessing is performed and no other processing is performed. The following command will generate the preprocessed program file (test.i).
gcc -E test.c -o test.i

2. Compilation phase

At this stage, gcc will first check whether the code has grammatical errors and whether it is standard. After checking it is correct, gcc translates the code into assembly language. The "gcc -S" option means only compile and generate assembly code (test.s file).
gcc -S test.i -o test.s

3. Assembly stage

At this stage, the compiled assembly file is converted into an OBJ object file. The "gcc -c" option means just compile without linking, and generate the object file "test.o"
gcc -c test.s -o test.o

4. Link phase

In this stage, link the OBJ object file generated in the assembly stage, the OBJ file of the system library, and the library file to finally generate an executable program. For example, to realize printf printing here, it is through the link function library.
gcc test.o -o test

Guess you like

Origin blog.csdn.net/sxtdzj/article/details/103453061