1. Simple programming steps under Ubuntu
-
Create a .c file
touch hello.c
-
into the C program file
vi hello.c
-
Write C language program
-
Save the edited C file
:wq
-
compile
gcc hello.c
-
execute program
./a.out
Two, GCC compiler
gcc (GNU CCompiler) is a multi-platform compiler with powerful functions and superior performance launched by GNU. The gcc compiler can compile and link C and C++ language source programs into executable files. Preprocessing, compiling, assembling and linking.
2.1 Preprocessing
gcc -E hello.c -o hello.i to get the preprocessing file , where -E means only preprocessing.
The source file will be generated by the compiler in the preprocessing stage. i file, which mainly deals with the instructions starting with # in the code. Such as: expand the macro definition, expand the header file, and remove the comment at the same time.
2.2 compile
gcc -S hello.i -o hello.s to get the assembly file , where -S means to generate only the assembly file.
Compilation is the process of syntactic analysis, lexical analysis, semantic analysis and optimization to generate corresponding assembly code files after the preprocessed files. This process is the core process of the entire program construction and is also the most complicated part.
2.3 Assembly
gcc -c hello.s -o hello.o where -c means only compiling and not linking. Convert the assembly code into an instruction file that the machine can execute, that is, an object file .
It can also be used directly: gcc -c hello.c -o hello.o outputs the target file directly after preprocessing, compiling and assembling.
2.4 link
gcc hello.o -o hello is the process of collecting and combining various pieces of code and data into an executable file , which can be loaded into memory for execution.