Compiler's working procedure

Source: Internet
Author: User

It may be a little late to see this now, but learn a little bit! This digest from Stemon http://www.cnblogs.com/stemon/p/3999844.html

To put it simply, in fact, to understand what the CPP file is different from the header file, you first need to understand the compiler's work process, in general the compiler will do the following several procedures:

1. Pre-processing phase (often said token)

2. Lexical and grammatical analysis phase

3. Compile phase, first compiled into a pure assembly statement, and then compiled into a CPU-related binary code, generate each target file (. obj file)

4. The connection phase, the individual target files in each section of the code for absolute address positioning, to generate a specific platform-related executables , of course, the final can also be used to generate pure binary code objcopy, that is, the removal of file format information. (Generate. exe file)

The compiler is compiled with a CPP file, which means that if none of your projects have a CPP file, your project will not compile. A connector is a target file , which takes one or more target files to reposition functions and variables (to determine the address of each function and variable relative to the beginning of the program, which is not a true memory address, The address of the real memory waits until the program loader determines the address of a register, generating the final executable file. program development on the PC, generally have a main function, which is the convention of each compiler. Of course, if you write your own connector script, you can not use the main function as a program entrance!!!!

(Main. c File target file executable file)

With this basic knowledge, and then, in order to generate a final executable file, you need some target files, that is, CPP files, and these CPP files need a main function as an executable program entrance, then we start from a CPP file, Assume that this CPP file contains the following:

MAIN.C function

#include <stdio.h>#include"Mytest.h"int main (int argc,char * *argv) {test =25; printf"test.................%d\n ",test); return 0;}

The contents of the Mytest.h header file are as follows:

int test;

Now let's take this example to explain the compiler's work:

1. Pre-processing stage: the compiler to the CPP file as a unit, first read the CPP file, found that the first sentence and the second sentence contains a header file, will be in all search paths to find the two header files, found, will go to the corresponding header file to deal with macros, variables, function declarations, Nested header files, such as detection of dependencies, macro substitution, to see if there is a duplicate definition and declaration of the situation, and finally all of those files are scanned into the current CPP file, the formation of an intermediate "CPP file."

In this step the equivalent of scanning the test variable in the header file into an intermediate CPP file, the test variable becomes a global variable in the file . in stdio.h This header file has some function declarations, then also the declaration of these functions of a brain scan into the intermediate CPP file (just scanned the function declaration, and did not implement).

2. Compile phase: This is the intermediate CPP file for all variables, function parameters to allocate space (in principle, only see the. h file in the declaration of functions, variables, for variables and functions, such as formal parameters allocated space), the functions are compiled into binary code, The target file is generated according to the specific target file format, in which the various global variables, the symbolic description of the function (the compiler maintains a symbolic description table)are organized into a target file according to certain standards.

At this time, each CPP file is compiled into a target file by the compiler, and each target file has a symbol table, this symbol table records the use of the CPP file which variables, which functions, function parameters are what type, there are several parameters. It also opens up memory space for variables and function parameters. So here the compiler put all the things you use CPP to record, if there are duplicate definitions or undefined variables, functions, etc., the compiler will know all of a sudden.

3. Connection stage: The previous step genetic the various target files, according to some parameters, the connection to generate the final executable file, the main task is to relocate the various target file functions, variables, etc., equivalent to the binary code in a target file according to a certain specification into a file.

Compiler's working procedure

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.