Learning C-GCC compilation process and common compilation options with cainiao

Source: Internet
Author: User
Tags comparison table

In the previous article, I learned that C code is stored in the memory after compilation. What is the entire compilation process of C code? A command of GCC hello. C can be compiled into executable program A. Out, And then the code of the program Hello. C can be executed after./A. Out. The following article has a good analysis and I have sorted it out.

Hello. C:

#include<stdio.h>int main(){        printf(“Hello World\n”);        return 0;}

In fact, GCC hello. C can be divided into four steps: preprocess, compilation, assembly, and linking ).

 

I. Preprocessing

The Preprocessing process mainly reads the C source program and processes pseudoinstructions and special symbols. Including macros, Conditional compilation, including header files, and some special symbols. Basically, it is a Replace Process.

gcc –E hello.c –o hello.i

The content of the pre-processed output file hello. I is as follows:

#1 "hello. C "#1" <built-in> "#1" <command-line> "#1" hello. C "#1"/usr/include/stdio. H "1 3 4 #28"/usr/include/stdio. H "3 4/***** omitted some content, including stdio. some declarations and definitions in H *****/#2 "hello. C "2int main () {printf (" Hello world \ n "); Return 0 ;}

The main processing rules for preprocessing are as follows:

1. Delete all # define statements and expand all macro definitions;

2. process all Conditional compilation commands, such as # If, # ifdef;

3. process the # include pre-compilation command to insert the included files to the pre-compilation command. This process is performed recursively, And the included files may contain other files.

4. Delete all comments // and /**/;

5. Add the row number and file ID, for example, #2 "hello. C "2, so that the compiler can generate the line number information for debugging during compilation and display the line number information during compilation errors or warnings;

6. Keep all # pragma compiler instructions because the compiler needs to use them;

 

Ii. Compilation

Through lexical and syntax analysis, the compilation process confirms that all commands comply with the syntax rules (otherwise, a compilation error is reported) and then translated into the corresponding intermediate code, in Linux, it is called RTL (register transfer Language), which is usually platform-independent. This process is also called the compile front-end. The compilation backend reduces and optimizes the RTL tree to obtain the assembly code that can be executed on the target machine. GCC uses as its assembler, so the assembler code is in at&t format, rather than intel format. Therefore, the at&t format is also used when compiling Embedded Assembly with GCC.

gcc –S hello.i –o hello.s

The contents of the compiled output file hello. s are as follows:


    .file  "hello.c"        .section    .rodata.LC0:        .string      "HelloWorld"        .text.globl main        .type         main, @functionmain:        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: (GNU)4.4.0 20090506 (Red Hat 4.4.0-4)"        .section   .note.GNU-stack,"",@progbits

 

Iii. 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

Because the content of Hello. O is a machine code, it cannot be conveniently presented in text format.

 

Iv. Links

The linker LD assembles each target file to resolve the symbolic dependency, library dependency, and generate executable files.

ld –static crt1.o crti.o crtbeginT.ohello.o –start-group –lgcc –lgcc_eh –lc-end-group crtend.o crtn.o

(The path name of the file is omitted ).

 

Of course, the static Link Library and dynamic Connection Library will also be used during the link. Both the static library and the dynamic library are a set of. O target files.

The static library extracts the relevant code during the link process and adds it to the executable file library (that is, when linking, the code of the function will be copied from the static link library where it is located to the final executable program ), ar only aggregates some other files into one file. You can package or unpackage.

ar -v -q  test.a test.o

The above command can generate a static Link Library test.

 

When a dynamic library is linked, only some symbol tables are created. When running, the code of the relevant library is loaded into the memory and mapped to the virtual address space of the corresponding process during running. If an error occurs, if the corresponding. So file cannot be found, a dynamic connection error will be reported during execution (LD_LIBRARY_PATH can be used to specify the path ). Use File test. So to see that test. So is the ELF File of shared object.

gcc -sharedtest.so test.o

The above command can generate a dynamic Connection Library test. So

 

Well, the entire compilation process is as shown above, so there are some compilation options for GCC. The details are as follows:

 

GCC compilation options

1.-C

Compile and generate an object file (*. OBJ) instead of linking to executable files, you can use this option when compiling several independent modules and linking them together by the linking program later, such:

         gcc -c hello.c ===> hello.o         gcc hello.o

 

2.-o

Allows you to specify the output file name, as shown in figure

           gcc hello.c -o hello.o           or           gcc hello.c -o hello 

3.-G

Indicates that the compilation program should generate debugging information in the compilation output. This debugging information allows source code and variable name reference in the debugging program or can be used when the program exits abnormally when it analyzes the core file.

 

4.-d

Allow defining macro characters from the compiled program command line

There are two cases: one is using-dmacro, which is equivalent to using # define macro in the program, and the other is using-dmacro = A, which is equivalent to # define macro A in the program. for example, the following code:

           #ifdef DEBUG                printf("debugmessage\n");           #endif

The-ddebug parameter can be added during compilation, and the execution program prints the compilation information.

 

5.-I

You can specify other locations for the include file. For example, if some include files are in special locations, such as/usr/local/include, you can add the following options:

           gcc -c -I/usr/local/include -I/opt/include hello.c

In this case, the Directory Search is performed in the given order.

 

6.-e

This option is relatively standard. It allows you to modify the command line so that the compiler sends the pre-processed C file to the standard output without actually compiling the code. this is useful when viewing C pre-processing pseudo commands and C macros. possible compilation outputs can be redirected to a file again and analyzed using the editing program:

           gcc -c -E hello.c >cpp.out   

This command causes the include file and program to be pre-processed and redirected to the file CPP. Out. Later, you can use the editing program or paging command to analyze the file and determine what the final C language code looks like.

 

7.-o

Optimization option. This option is not standard.

-O and-O1 Specify level 1 Optimization

-O2: Level 2 Optimization

-O3: Level 3 Optimization

-The O0 parameter is not optimized.

Gcc-C O3-O0 hello. c

When there are multiple optimizations, take the last one as the standard !!

 

8.-wall

Use GNU to compile programs at the highest level, specifically for displaying warnings !!

           gcc -Wall hello.c

 

9.-l

Specify the search directory for the connected database, and-l (lower case l) specify the name of the connected database

           gcc main.o -L/usr/lib -lqt -o hello
 

10.-Share

This option will try to use the dynamic library, so the file generation is small, but the system needs

 

11.-static

This option will disable the use of dynamic libraries. Therefore, the compiled items are usually large and no dynamic Connection Library is required.

 

12.-FPIC

Indicates that the compiled code is a code with independent locations. Without this option, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, but cannot achieve the purpose of truly sharing code segments.

 

 

 

 

Related Article

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.