gcc/g++ compiler compiler principles: pre-processing, compiling, compiling, linking the steps of the detailed

Source: Internet
Author: User

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:

    1. Pretreatment (preprocessing)-----------------CPP/GCC-E
    2. Compile (compilation)------------------Cc1/gcc-s
    3. Assembly (assembly)--------------------as
    4. 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?
    1. #include <stdio.h>
    2. int main ()
    3. {
    4. printf ("hello!  This was our embedded world!\n ");
    5. return 0;
    6. }

(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?
  1. typedef INT (*__GCONV_TRANS_FCT) (struct __gconv_step *,
  2. struct __gconv_step_data *, void *,
  3. __const unsigned char *,
  4. __const unsigned char * *,
  5. __const unsigned char *, unsigned char * *,
  6. size_t *);
  7. ...
  8. # 2 "hello.c" 2
  9. int main ()
  10. {
  11. printf ("hello!  This was our embedded world!\n ");
  12. return 0;
  13. }

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?
  1. . File "hello.c"
  2. . section. Rodata
  3. . Align 4
  4. . LC0:
  5. . String "hello! This is our embedded world! "
  6. . text
  7. . GLOBL Main
  8. . type Main, @function
  9. Main
  10. PUSHL%EBP
  11. MOVL%esp,%EBP
  12. Subl $8,%esp
  13. Andl $-16,%esp
  14. MOVL,%eax
  15. Addl,%eax
  16. Addl,%eax
  17. Shrl $4,%eax
  18. Sall $4,%eax
  19. Subl%eax,%esp
  20. SUBL,%esp
  21. PUSHL $. LC0
  22. Call puts
  23. Addl $16,%esp
  24. MOVL,%eax
  25. Leave
  26. Ret
  27. . size main,.-main
  28. . Ident "GCC: (GNU) 4.0.0 20050519 (Red Hat 4.0.0-8)"
  29. . 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?
    1. The method of generating a static library:   
    2.     ar  cr libxxx.a file1.o file2.o  
    3. when used   
    4.     gcc test.c -l/ path -lxxx -o test  
    5.   
    6. Dynamic library:   
    7.     gcc -fpic -shared file1.c -o libxxx.so  
    8. Li class= "alt" can also be divided into two parts to write:   
    9.     gcc -fPIC file1.c -c  //this step generates file1.o  
    10. &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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.