The following is a brief summary of how to compile Hellworld with GCC.
Hello. cSource codeAs follows:
# Include <stdio. h>
Int main ()
{
Printf ("Hello, world. \ n ");
Return 0;
}
We usually use GCC to generate executable code.ProgramCommand: GCC hello. C. The Executable File A. Out is generated by default.
In fact, the command for compiling (including links): GCC hello. C can be divided into the following four major steps:
· Preprocessing)
· Compile)
· Assembly)
· Link (linking)
1. Preprocessing)
The Preprocessing process mainly includes the following processes:
Delete all # define statements and expand all macro definitions.
Process all conditional pre-compiled commands, such as # If # ifdef # Elif # else # endif.
Processing # include pre-compilation instructions: insert included files to the pre-compilation instructions.
Delete all comments "//" and "/**/".
Add a line number and a file ID to generate a line number for debugging and a warning line number for compilation errors during compilation.
Keep all # pragma compiler instructions because the compiler needs to use them
The following commands are usually used for preprocessing:
Gcc-e hello. C-O hello. I
The-e parameter indicates that only preprocessing is performed or the following command can be used to complete the preprocessing process.
CPP hello. c> hello. I/* CPP-the C Preprocessor */
Directly cat hello. I, you can see the pre-processedCode
2. Compile (Compilation)
The compilation process is to perform a series of lexical analysis, syntax analysis, Semantic Analysis and Optimization on the pre-processed files to generate the corresponding assembly code.
$ Gcc-s hello. I-O hello. s
Or
$/Usr/lib/GCC/i486-linux-gnu/4.4/PC3 hello. c
Note: In the current version of GCC, the preprocessing and compilation steps are combined into one step, which is completed by using the tool "cc1. GCC is actually some packaging of background programs, called according to different parametersOthersThe actual processing program, such as: Pre-compiled program, assembler as, connector LD
The compiled assembly code (hello. s) is as follows:
. File "Hello. c"
. Section. rodata
. Lc0:
. String "Hello, world ."
. Text
. Globl main
. Type main, @ Function
Main:
Pushl % EBP
Movl % ESP, % EBP
Andl $-16, % ESP
Subl $16, % ESP
Movl $. lc0, (% ESP)
Call puts
Movl $0, % eax
Leave
RET
. Size main,.-Main
. Ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
. Section. Note. GNU-stack, "", @ progbits
3. Assembly)
Assembler converts assembly code into commands that can be executed by machines. Each assembly statement corresponds to almost one machine command. Compared with the compilation process, the compilation process is relatively simple. You can translate the compilation commands one by one based on the comparison table of the Assembly commands and machine commands.
$ Gcc-C hello. C-O hello. o
Or
$ As hello. S-O hello. co
Because the content of Hello. O is a machine code, it cannot be viewed in the form of common text (if you open the Vi, you will see garbled code ).