A brief analysis of C program compiling process

Source: Internet
Author: User

A few days ago saw "programmer self-accomplishment--link, load and library" in the second chapter of "Compile and link", mainly based on the contents of a simple summary of C program compilation process it.

I now generally use gcc, so naturally gcc compiled hellworld as an example, briefly summarized as follows.

HELLO.C source code is as follows:

/**/intreturn0; }

Usually we use GCC to generate executable programs, the command is: GCC hello.c, the default generation of executables A.out

In fact, the command to compile (including links): gcc hello.c can be decomposed into the following 4 large steps:

      • pretreatment (preprocessing)
      • compiling (compilation)
      • compilation (Assembly)
      • links (linking)

GCC compilation



1. pretreatment (preproceessing)

The process of preprocessing mainly involves the following processes:

    • Delete all # define, and expand all macro Definitions
    • handles all conditional precompiled directives , such as # if #ifdef #elif #else #endif等
    • processes the # include precompiled directive, inserting the contained file into the location of the precompiled instruction.
    • Delete all comments "//" and "/* * */".
    • Add line numbers and file identities so that compile-time generates debug line numbers and compile error warning line numbers.
    • Keep all #pragma编译器指令because the compiler needs to use them

The following commands are typically used for preprocessing:

GCC-E Hello.c-o hello.i

The parameter- e means that preprocessing is done only or can be completed using the following directives

CPP hello.c > HELLO.I/* cpp–the C Preprocessor */

Direct Cat hello.i You can see the preprocessed code

2. Compiling (Compilation)

The process of compiling is a series of lexical analysis, grammatical analysis, semantic analysis and optimization of the pre-processed files into corresponding assembly codes.

$GCC –S Hello.i–o Hello.s

Or

$/usr/lib/gcc/i486-linux-gnu/4.4/cc1 hello.c

Note: The current version of GCC synthesizes the two steps of preprocessing and compiling in a single step, which is done with the CC1 tool. GCC is actually some of the background program packaging, according to different parameters to invoke other actual processing programs, such as: Precompiled compiler cc1, assembler as, connector ld

You can see the compiled assembly code (HELLO.S) as follows: ASSEMBLY

. File "Hello.c"
. section. Rodata
. LC0:
. String "Hello, World."
. text
. GLOBL Main
. type Main, @function
Main
PUSHL%EBP
MOVL%esp,%EBP
Andl $-16,%esp
Subl $16,%esp
MOVL $. LC0, (%ESP)
Call puts
MOVL,%eax
Leave
Ret
. size main,.-main
. Ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
. section. Note. Gnu-stack, "", @progbits

3. Compilation (Assembly)

A assembler is a command that transforms assembly code into a machine executable that almost every assembly statement corresponds to a machine instruction. The assembly is relatively simple compared to the compilation process, according to the assembly instructions and machine instructions for the comparison table one by one translation can be.

$ gcc–c Hello.c–o hello.o

Or

$ as Hello.s–o hello.co

Because the content of HELLO.O is machine code, cannot view in plain text form (vi opens to see is garbled).

4. Links (linking)

The linker ld is called to link a large stack of target files needed for the program to run, as well as other library files that are dependent on it, and finally the executable file is generated.

Ld-static crt1.o crti.o crtbegint.o hello.o-start-group-lgcc-lgcc_eh-lc-end-group crtend.o crtn.o (the path name of the file is omitted).

This is how the HelloWorld compiles and links process, so what exactly does the compiler and linker do?

The compilation process can be divided into 6 steps: scanning (lexical analysis), parsing, semantic analysis, source code optimization, code generation, and target code optimization.

Lexical analysis: the scanner (Scanner) divides the character sequence of the source generation into a series of tokens. The Lex tool enables lexical scanning.

Parsing: The parser generates tokens (tokens) into a syntax tree (Syntax tree). The YACC tool enables parsing (Yacc:yet another Compiler Compiler).

Semantic analysis: Static semantics (semantics that can be determined by the compiler), dynamic semantics (semantics that can only be determined at run time).

Source code optimization: source code Optimizer, which translates the entire grammar book into intermediate Code (intermediate Code) (the intermediate codes are independent of the target machine and the running environment). The intermediate code allows the compiler to be divided into front and back ends. The compiler's front-end is responsible for generating machine-independent intermediate code, and the compiler backend translates the intermediate code into the target machine code.

Target code generation: code Generator.

Target code optimization: Target Code Optimizer.

The main content of the link is to deal with the parts of each module that are referenced to each other, so that the modules can be properly connected to each other.

The main processes of linking are: address and space allocation (addresses and Storage Allocation), symbol resolution (symbol Resolution), relocation (relocation), etc.

Links are divided into static links and dynamic links.

static linking is the process of adding a static library to an executable file directly during the compile phase, so that the executable file is relatively large.

dynamic Link refers to the link stage only to add some descriptive information, and the program executes the corresponding dynamic library from the system to load into memory.

The approximate procedure for static linking is as follows:

static linking

Resources:

"Programmer's self-accomplishment--link, load and library"

Recommendation: http://www.cnblogs.com/roucheng/p/3454292.html

A brief analysis of C program compiling process

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.