The compiling principle and process __c language of C language

Source: Internet
Author: User

A few days ago a friend asked me about the C language compiler principle and compiling process, at that time I did not explain white, today deliberately in the book and online access to information, a simple summary of the C language of the compiler principle and process.

An integrated development environment is an application that is used to provide a program development environment, typically including a code editor, a compiler, a debugger, and a graphical user interface tool. Integration of code writing functions, analysis functions, compile functions, debugging functions, such as integrated development software service sets.

Editor: Some windows that write code.

Compiler: Check the user code for some syntax errors and compile it into assembly code.

Assembler: Compile the compiled file into a certain function of the target code.

Connector: Connects the target code to an executable file.


A compiler consists of several parts:

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

2. Syntax Analysis: The parser produces the syntax tree (Token) with the notation (Syntax). The YACC tool enables parsing (Yacc:yet Another Compiler Compiler).

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

4. Source code optimization: Source code Optimizer (Optimizer) converts the entire grammar book into intermediate Code (intermediate Code) (intermediate codes are not related to the target machine and the operating environment). The intermediate code allows the compiler to be divided into front-end and back-end. The compiler front-end is responsible for generating machine-independent intermediate code, and the compiler back end converts the intermediate code into the target machine code.

5. Target code generation: code generator.

6. Target code optimization: Target Code optimizer (Optimizer).


After understanding the above several concepts, formally into the C language compiler principles and processes:

C language Compilation Complete process: C source program---> Precompiled processing---> Compiling, optimizing processing---> Assembler---> Link program---> Executable file

The above process can be easily divided into 4 major steps: Preprocessing---> Compiling---> Compiling---> Links

1. pretreatment: including the following processes

A. Macro definition directive: Remove all #define and expand all macro definitions

B. Conditional compilation directives: Handle all conditional precompiled instructions, such as #if #ifdef #elif #else #endif等

C. header file contains directives: processing #include instructions, inserting the included files into the precompiled instruction location

D. Special symbolic directives: pre-compilers can identify special symbols, such as: Delete all comments "//" and "*/*"

E. Add line numbers and file identities to generate debug line numbers and compile error warning line numbers at compile time

F. Keep all #pragma compiler directives because the compiler needs to use them

Use the following command to Preprocess:

GCC-E *.c-o *.i

Parameter- e indicates preprocessing only

2. Compile : The process of compiling is to do a series of lexical analysis, grammatical analysis, semantic analysis and optimization of the pre processed files into the corresponding assembly code.

Compile with the instructions:

Gcc–s *.i–o *.s

3. Assembler: an assembler is a command that converts assembly code into a machine that can be executed, and almost every assembly statement corresponds to a single machine instruction. According to the assembly instructions and machine instructions of the comparison table one by one translation can be. Use instructions to assemble:

Gcc–c *.c–o *.O

4. Link: by invoking the linker LD to link the program to run the need for a large number of target files, as well as other library files on the dependent, and finally build the executable file. The main content of the link is to handle the parts of each module, so that the modules can be properly connected. Links are divided into static links and dynamic links.

Static linking refers to adding a static library to an executable file in the compile phase, so that the executable file is larger. Static library file: is a binary file, stored function implementation, in the file compiled to access the file, after compiling the static library files can be deleted

and dynamic link refers to the link phase only add some descriptive information, and the program execution and then from the system to load the corresponding dynamic library into memory, dynamic library file: is a binary file, storage function implementation, file execution to access the file, compile without the need for dynamic library files

Linux dynamic library file suffix name:. so
window dynamic library file suffix name:. dll

Target file: is a binary file, composed of functions that cannot be executed individually, to a file call to the main function.





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.