Excerpted from http://blog.csdn.net/elfprincexu/article/details/45043971
gcc/g++ compiler compiler principles: pre-processing, compiling, compiling, linking the steps of the detailed
C and C + + compilers are integrated, and compilation is generally divided into four steps:
- Pretreatment (preprocessing)-----------------CPP/GCC-E
- Compile (compilation)------------------Cc1/gcc-s
- Assembly (assembly)--------------------as
- Connection (linking)---------------------LD
Gcc
The file that is considered preprocessed is (. i) is a C file, and the connection is set in C form;
g++
It is considered that the preprocessed file is (. i) a C + + file, and the connection is set in C + +;
Some meanings of the source file suffix name and subsequent operations:
- . C C Source program preprocessing, compilation, compilation
- . C C + + source program preprocessing, compilation, compilation
- . CC C + + source program
- . cxx C + + source program preprocessing, compiling, compiling
- . m objective-c Source program preprocessing, compilation, compilation
- . I pre-processing C file compilation, compilation
- . II post-processing C + + file compilation, compilation
- Assembly of the. S assembly Language source program
- . S assembly Language Source program preprocessing, compilation
- . h preprocessor files typically do not appear on the command line
Files with other suffix names are passed to the connector (linker). Typically include:
. o Destination file (object files)
. A archive library files (Archive file)
Reprint Please specify source: Http://blog.csdn.net/elfprincexu
Second, specific introduction of the GCC compilation steps
First, there are the following hello.c source code
[CPP]View PlainCopy print?
- #include <stdio.h>
- int main ()
- {
- printf ("hello! This was our embedded world!\n ");
- return 0;
- }
(1) pretreatment stage
At this stage, the compiler compiles the stdio.h in the code above, and the user can view it using the GCC option "-e", which is to let GCC stop the compilation process after the preprocessing is finished. The preprocessing phase mainly deals with # include and # define, which inserts the. h file with # include in the location of # include, and replaces the macro defined by # define with the actual string in the source program. We can use the-e option to require GCC to be preprocessed only and not perform the next three stages.
Note: The general format of the GCC directive is: gcc [options] file to be compiled [options] [target file]
Where the target file is default, GCC generates the executable file by default: Compile the file.
[Email protected] gcc]# gcc–e hello.c–o hello.i
Here, the option "-O" refers to the target file, and the ". I" file is a pre-processed C primitive program. The following is a list of some of the contents of the Hello.I file:
[CPP]View PlainCopyprint?
- 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 was our embedded world!\n ");
- return 0;
- }
This shows that GCC does have preprocessing, which inserts the contents of "stdio.h" into the hello.i file.
(2) Compile stage
The next step is the compile phase, in which GCC first checks the code for normalization, syntax errors, and so on, to determine what the code actually does, and after the check is correct, GCC translates the code into assembly language. Users can view it using the "-S" option, which compiles without compiling, generating assembly code.
The compile phase is the most important phase, in which GCC first checks the syntax and then compiles the *.i generated by the previous step into the *.s file. We can tell GCC to do this step with the following command, Gcc-s hello.i-o hello.s,-s option tells GCC to compile hello.i into. S files;
The output files for both steps above are text files, and we can read them using commands such as cat text processing. This stage can receive. C and. I types of files
[Email protected] gcc]# gcc–s hello.i–o Hello.s
The contents of Hello.s are listed below, and it is clear that GCC has turned it into a compilation, and interested readers can analyze how this simple C-language applet is implemented with assembly code.
[CPP]View PlainCopyprint?
- . 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,%eax
- Addl,%eax
- Addl,%eax
- Shrl $4,%eax
- Sall $4,%eax
- Subl%eax,%esp
- SUBL,%esp
- PUSHL $. LC0
- Call puts
- Addl $16,%esp
- MOVL,%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
The assembly phase translates the *.s file into a binary machine instruction file *.o, such as command gcc-c Hello.s-o hello.o, where-C tells GCC to process the assembly. This step generated file is a binary file, directly opened with a text tool to see will be garbled, we need disassembly tool such as GDB's help to read it; this stage receives. C,. I,. s files are all OK. such as Gcc-c Hello.i-o hello.o, etc.
The assembler stage is to convert the ". S" file generated during the compilation phase to the target file, where the reader can use the option "-C" to see the binary target code that the assembly code has converted to ". O". As shown below:
[Email protected] gcc]# gcc–c hello.s–o hello.o
(4) Link stage
After a successful compilation, the link stage is entered. Here comes an important concept: the library of functions.
Readers can re-view this applet, in this program does not define "printf" function implementation, and in the pre-compilation included in the "Stdio.h" also only the function declaration, but not the implementation of the definition function, then, where is the implementation of "printf" function? The final answer is: the system to the implementation of these functions are known as the Libc.so.6 library file, when not specifically specified, GCC will go to the system default search path "/usr/lib" to find, that is, link to the libc.so.6 library function, so that the function can be implemented " printf ", and that's what links are for.
function library is generally divided into static library and dynamic library two kinds.
- Static library refers to the compilation of links, the library files of the code are all added to the executable file, so the resulting file is relatively large, but at run time also no longer need the library file. The suffix is generally ". a".
- Dynamic libraries In contrast, when compiling a link does not add the code of the library file to the executable file, but the library is loaded by the runtime link file when the program executes, which can save the system overhead. The general suffix of the dynamic library is named ". So", as described in the previous libc.so.6 is a dynamic library. GCC uses dynamic libraries by default at compile time.
[CPP]View PlainCopyprint?
- The method of generating a static library:
- ar cr libxxx.a file1.o file2.o
-
- when used
- gcc test.c -l/ path -lxxx -o test
-
- Dynamic library:
- gcc -fpic -shared file1.c -o libxxx.so
Li class= "alt" can also be divided into two parts to write:
- gcc -fPIC file1.c -c //this step generates file1.o
- &NBSP;&NBSP;&NBSP;&NBSP;GCC&NBSP;-SHARED&NBSP;FILE1.O&NBSP;-O&NBSP;LIBTEST.SO&NBSP;&NBSP;
The effect is the same. Use the same time as the static library above, but to run the program, you need to specify the location of the dynamic library, you can specify export ld_library_path=path environment variables, otherwise you will not find the location of the dynamic library Because the methods used to link the dynamic and static libraries are the same, if you have static library files and dynamic library files with the same name in the library, For example, LIBTEST.A and libtest.so, according to the GCC link when the default preference dynamic library, will link libtest.so, if you want GCC to choose link LIBTEST.A then you need to specify an option, that is-static, This will force GCC to find the static library file.
Search path order when static library links:
- 1. LD will go to the parameters in the GCC command-l
- 2. Re-search for GCC environment variables Library_path
- 3. Find the default directory/lib/usr/lib/usr/local/lib This is the original compile GCC when written in the program
Dynamic Link-time, execution-time search path order:
- 1. The dynamic library search path specified when compiling the target code
- 2. Environment variable LD_LIBRARY_PATH the specified dynamic library search path
- 3. The dynamic library search path specified in configuration file/etc/ld.so.conf
- 4. Default dynamic library search path/lib
- 5. Default dynamic Library search path/usr/lib
About environment variables:
- Library_path environment variable: Specifies the program static link library file search path
- LD_LIBRARY_PATH environment variable: Specifies the program dynamic link library file search path
Once the link has been completed, GCC can generate the executable file, as shown below.
[Email protected] gcc]# Gcc hello.o–o Hello
Run the executable file and the correct results appear as follows.
[Email protected] gcc]#./hello
Hello! Our embedded world!
gcc/g++ compiler compiler principles: pre-processing, compiling, compiling, linking the steps of the detailed