C Program compilation process

Source: Internet
Author: User

is generally used with GCC, so the natural GCC compiled Hellworld as an example, briefly summarized as follows:

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)

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

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

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.

C Program compilation 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.