I. Overview
C and C ++ compilers are integrated. Compilation is generally divided into four steps:
- Preprocessing)
- Compile)
- Assembly)
- Connection (linking)
Gcc
The pre-processing file (. I) is a C file, and the C-form connection is set;
G ++
It is considered that the pre-processed file is (. I) A C ++ file, and a connection in the C ++ form is set;
Some Meanings of the source file suffix and subsequent operations:
- . C source program preprocessing, compilation, compilation
- . C ++ source program preprocessing, compilation, compilation
- . Cc c ++ source code
- . Cxx C ++ source program preprocessing, compilation, and assembly
- . M objective-C source program preprocessing, compilation, assembly
- . I pre-processed C file compilation and compilation
- . II pre-processed C ++ file compilation and compilation
- . S assembler language source program assembly
- . S assembler language source program preprocessing, assembly
- . H pre-processor files usually do not appear on the command line
Other suffixed files are passed to the connector (linker). These files generally include:
. O target file)
. A archive file)
Ii. Detailed GCC compilation steps
First, the following hello. C source code is available:
#include<stdio.h>
int main()
{
printf("Hello! This is our embedded world!\n");
return 0;
}
(1) preprocessing stage
At this stage, the compiler adds stdio. H is compiled, and you can use the GCC option "-e" for viewing. This option is used to stop the GCC compilation process after preprocessing.
Note:
The general format of GCC commands is: GCC [Option] file to be compiled [Option] [target file]
The target file can be defaulted. By default, GCC generates executable files, and the output is: Compile file. Out.
[Root @ localhost GCC] # gcc-e hello. C-O hello. I
Here, the option "-o" refers to the target file, and the ". I" file is the original C program that has been preprocessed. Some Contents of the hello. I file are listed as follows:
Typedef int (* _ gconv_trans_fct) (struct _ gconv_step *,
Struct _ gconv_step_data *, void *,
_ Const unsigned char *,
_ Const unsigned char **,
_ Const unsigned char *, unsigned char **,
Size_t *);
...
#2 "Hello. c" 2
Int main ()
{
Printf ("Hello! This is our embedded world! \ N ");
Return 0;
}
It can be seen that GCC does perform preprocessing, And it inserts the content of "stdio. H" into the hello. I file.
(2) compilation phase
The next step is the compilation phase. In this phase, GCC should first check the code standardization and whether there are syntax errors to determine the actual work to be done by the Code, GCC translates the code into an assembly language. You can use the "-s" option to view the Code. This option is only compiled but not compiled to generate the assembly code.
[Root @ localhost GCC] # gcc-s hello. I-O hello. s
Hello. s content, it can be seen that GCC has converted it into assembly, interested readers can analyze how this line of simple C language applets are implemented using assembly code.
. File "Hello. c"
. Section. rodata
. Align 4
. Lc0:
. String "Hello! This is our embedded world! "
. Text
. Globl main
. Type main, @ Function
Main:
Pushl % EBP
Movl % ESP, % EBP
Subl $8, % ESP
Andl $-16, % ESP
Movl $0, % eax
Addl $15, % eax
Addl $15, % eax
Shrl $4, % eax
Sall $4, % eax
Subl % eax, % ESP
Subl $12, % ESP
Pushl $. lc0
Call puts
Addl $16, % ESP
Movl $0, % eax
Leave
RET
. Size main,.-Main
. Ident "GCC: (GNU) 4.0.0 20050519 (Red Hat 4.0.0-8 )"
. Section. Note. GNU-stack, "", @ progbits
(3) Assembly stage
In the Assembly phase, the compilation phase is generated ". "S" file to the target file, the reader can use the option "-c" here to see that the assembly code has been converted ". o. As follows:
[Root @ localhost GCC] # gcc-C hello. S-O hello. o
(4) link stage
After successful compilation, it enters the link stage. This involves an important concept: function libraries.
Readers can re-view this applet. In this program, the function implementation of "printf" is not defined, and the "stdio. h "is only the declaration of the function, but does not define the implementation of the function. So where does it implement the" printf "function? The final answer is: The system implements these functions named libc. so.6 is included in the library file. If it is not specified, GCC will search for it under the default search path "/usr/lib", that is, link to libc. go to the so.6 library function to implement the function "printf", which is the role of the link.
Function libraries are generally divided into static libraries and dynamic libraries.
A static library is used to add all the code of the library file to the executable file when compiling the link. Therefore, the generated file is large, but the library file is no longer needed at runtime. The suffix is generally ". ".
On the contrary, the dynamic library does not add the code of the library file to the executable file during the compilation link, but loads the library from the link file during the program execution, this saves the system overhead. The dynamic library is generally suffixed with ". So". As mentioned above, libc. so.6 is the dynamic library. By default, dynamic libraries are used for GCC compilation.
After the link is complete, GCC can generate an executable file, as shown below.
[Root @ localhost GCC] # GCC hello. O-O hello
Run the executable file. The correct result is as follows.
[Root @ localhost GCC] #./Hello
Hello! This is our embedded world!