1. GCC compiler
(1) program compilation process
When using GCC to compile a program, the compilation process can be divided into four stages:
A. Pre-processing)
B. Compile (Compiling)
C. assembing)
D. Link (linking)
(2) In terms of functions, preprocessing, compilation, and assembly are three different stages.
A. in the pre-processing stage, the input is the source file of C language, usually *. C or *. C. They generally contain header files such as *. h. Main processing at this stage
# Ifdef, # include, # define preprocessing command in the source file. In this phase, an intermediate file *. I is generated.
Gcc-e tiger. C-O test. I
It uses the E Option for the source file tiger. C to generate the intermediate file tiger. I
B. In the compilation phase, enter the intermediate file * I. After compilation, generate the assembly language file * s. The GCC command corresponding to this phase is:
Gcc-s tiger. I-o tiger. s
C. In the Assembly stage, convert the input Assembly file *. s to the binary machine code *. O. The GCC command corresponding to this stage is:
Gcc-C tiger. S-O tiger. o
D. At the end of the link stage, the input binary machine code file *. O (in other machine code files and library files) is pooled into an executable binary code file. The Command Used in this phase is:
GCC tiger. O-o Tiger
Finally, an executable file tiger is generated.
(3) The preceding process can be simplified:
GCC tiger. C-o Tiger
(4) In actual development, when using GCC to compile the source program, there are usually more than one source file. In this case, you need to use GCC to compile multiple source files. Run the following command: OK
Gcc-O tiger tiger1.c tiger2.c tiger3.c
This command compiles three source files tiger1.c tiger. c tiger3.c at the same time, and finally generates an executable tiger.
Note: whether a program has only one or more source files, it must have only one main function in all the source files compiled and linked, because the main function is the entry point of every program.
3. Common GCC options
(1) Basic GCC usage
GCC [Options] [filenames]
Among them, options is the required option for the compiler, and filenames gives the relevant file name.
A. -C: Compile only, not link to the class execution file. The compiler is only input. c. O is a target file with a suffix. It is usually used to compile a subroutine file that does not contain the main program.
B.-O output_filename: determine that the output file name is output_filename. If this option is not used, GCC defaults to the source file name. o file.
C.-G: generate the symbolic information required by the Debugger GDB. To debug the source code, you must add this option when compiling the program.
D. -O: optimizes the compilation and linking of the program. With this option, the entire source code is compiled and optimized during the link process. In this way, the execution efficiency of executable files is high, however, compile and link
The speed is correspondingly slower.
E.-O2: better optimized compilation and linking than-o. The link process is slower during the entire compilation process.
F.-wall