Detailed process from C ++ source code to executable code, source code

Source: Internet
Author: User

Detailed process from C ++ source code to executable code, source code

Compile and compile the program to read the source program (ghost stream), analyze the lexical and syntax, convert advanced language commands into functional equivalent assembly code, and then convert the assembly program into machine language, and generate executable programs based on the requirements of the operating system for the executable file format.

Source code --> preprocessing --> Compilation --> optimization --> assembly --> link --> Executable File

Source -- (compile) --> Assembly -- (assemble) --> Obj -- (Link) --> PE/ELF



1. Preprocessing)

Reads the source program and processes the pseudocommands (commands starting with #) and special symbols.

The following commands are usually used for preprocessing:

Gcc-E hello. c-o hello. I

The-E parameter indicates that only preprocessing is performed or the following command can be used to complete the preprocessing process.

Cpp hello. c> hello. I/* cpp-The C Preprocessor */

Directly cat hello. I, you can see the pre-processed code.

[Analysis] pseudocommands mainly include the following four aspects:
(1) macro definition commands, such as # define Name TokenString and # undef. For the previous pseudo command, all the names in the program must be replaced by TokenString, but the Name as a String constant is not replaced. For the latter, the definition of a macro will be canceled, so that the appearance of the string will not be replaced in the future.
(2) Conditional compilation commands, such as # ifdef, # ifndef, # else, # elif, # endif, and so on. The introduction of these pseudo commands allows programmers to define different macros to determine which code the program will process. The pre-compiled program filters out unnecessary code based on the relevant files.
(3) the header file contains commands, such as # include "FileName" or # include <FileName>. In header files, a large number of macros (the most common is a character constant) are defined using a pseudo command # define, which also contains declarations of various external symbols. The purpose of using header files is to make some definitions available for multiple different C source programs. In the C source program that needs to use these definitions, you only need to add a # include statement, instead of repeating these definitions in this file. The precompiled program adds all the definitions in the header file to the output file generated by the precompiled program for the Compilation Program to process it.
The header files contained in the c source program can be provided by the system. These header files are generally stored in the/usr/include directory. # Include them in the program using angle brackets (<> ). In addition, developers can also define their own header files. These files are generally placed in the same directory as the c source program. In this case, double quotation marks ("") are used in # include ("").
(4) special symbols. Pre-compiled programs can recognize some special symbols. For example, the LINE mark in the source program will be interpreted as the current LINE number (in decimal number), and the FILE will be interpreted as the name of the currently compiled C source program. The pre-compiled program replaces these strings with appropriate values in the source program.
The Preprocessing Program basically replaces the source program. After this replacement, an output file without macro definition, Conditional compilation instructions, and special symbols is generated. The meaning of this file is the same as that of the source file without preprocessing, but the content is different. Next, the output file will be translated into machine commands as the output of the Compilation Program.


2. Compilation)

The compilation process is to perform a series of lexical analysis, syntax analysis, Semantic Analysis and Optimization on the pre-processed files to generate the corresponding assembly code.

$ Gcc-S hello. I-o hello. s

Or

$/Usr/lib/gcc/i486-linux-gnu/4.4/PC3 hello. c

Note: In the current version of GCC, the preprocessing and compilation steps are combined into one step, which is completed by using the tool "cc1. Gcc is actually some packaging of the background program. It calls other actual processing programs according to different parameters, such as: Pre-compiled program, assembler as, connector l

The compiler is compiled in units of c/c ++ files. If the project does not contain c/c ++ files, your project cannot be compiled. Only constants exist in the pre-compiled output file. Such as numbers, strings, variable definitions, and keywords in C language, such as main, if, else, for, while, {,}, +,-, *, \, and so on. To compile a program, perform lexical analysis and syntax analysis. After confirming that all commands comply with the syntax rules, translate them into equivalent intermediate code representation or assembly code.


3. Optimization stage
Optimization processing is a difficult technology in the compilation system. It involves not only the compilation technology itself, but also the hardware environment of the machine. The optimization part is the optimization of the intermediate code. This optimization does not depend on a specific computer. Another optimization is mainly for generating the target code. We put the optimization stage behind the Compilation Program, which is a general representation.

For the previous optimization, the main work is to delete public expressions, loop optimization (out-of-code optimization, weak strength, changing cycle control conditions, merging of known quantities, etc.), and re-write propagation, and the deletion of useless values.

The Optimization of the latter type is closely related to the hardware structure of the machine. The main consideration is how to make full use of the values of relevant variables stored in each hardware register of the machine, to reduce the memory access times. In addition, how to make some adjustments to commands based on the features of machine hardware execution commands (such as pipelines, Proteus, CISC, and VLIW) to make the target code relatively short and the execution efficiency relatively high, it is also an important research topic.

The compiled code after optimization must be compiled by the assembler and converted into corresponding machine commands, which may be executed by machines.

4. Assembly Process)

The assembly process refers to the process of translating the assembly language code into the target machine instructions. For each C language source program processed by the translation system, the corresponding target file will be obtained after this processing. What is stored in the target file is the machine language code equivalent to the source program.

$ Gcc-c hello. c-o hello. o

Or

$ As hello. s-o hello. co the target file consists of segments. Generally, a target file contains at least two segments:


The section in the code segment contains the program instructions. This section is generally readable and executable, but generally cannot be written.

The data segment mainly stores various global variables or static data used in the program. Generally, data segments are readable, writable, and executable.

There are three types of target files in UNIX:

(1) relocated files include code and data suitable for other target file links to create an executable or shared target file.

(2) shared target files store the code and data suitable for linking in two contexts. First, the linking program can process it with other relocated files and shared target files to create another target file; the second is that the dynamic link Program combines it with another executable file and other shared target files to create a process image.

(3) an executable file contains a file that can be executed by a process created by the operating system.

The assembler generates the first type of target file. For the last two methods, some other processing is required. This is the work of The Link program.

5. Link Program (Linking)

The target file generated by the assembler cannot be executed immediately. There may be many unsolved problems. For example, a function in a source file may reference a symbol (such as a variable or function call) defined in another source file, or call a function in a library file in a program. All these problems must be handled by the linked program.

Call the linker ld to link a large number of target files required for running the program, as well as other dependent library files, and finally generate executable files.

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 task of linking a program is to connect the target file to each other, or connect the symbols referenced in one file with the definition of the symbol in another file, this makes all these target files a unified whole that can be loaded and executed by the operating system.

Link processing can be divided into two types based on the connection methods specified by developers for functions of the same Library:

(1) Static links in this way, the code of the function will be copied from the static Link Library in its location to the final executable program. In this way, when the program is executed, the code will be loaded into the virtual address space of the process. The static Link Library is actually a collection of target files. Each file contains the code of one or more related functions in the library. (Personal note: the static link copies the code of the Linked Library to the executable program to increase the size of the executable program)

(2) In this way, the code of a function is put in a target file called a dynamic link library or shared object. The link stage only adds some description information, and the corresponding dynamic library is loaded into the memory during program execution. What the linked program does at this time is to record the name of the shared object and a small amount of other registration information in the final executable program. When the executable file is executed, all content of the dynamic link library will be mapped to the virtual address space of the corresponding process at runtime. The dynamic link program finds the corresponding function code based on the information recorded in the executable program. (Personal note: a dynamic link refers to the Code to be linked to a shared object. The shared object is mapped to the virtual address space of the process, the linked program records the code information that can be used by the executable program in the future, and quickly locates the corresponding code snippets based on the information .)

For function calls in executable files, dynamic or static links can be used respectively. Dynamic Links can make the final executable files relatively short, and save some memory when the shared object is used by multiple processes, because only one copy of the shared object code needs to be saved in the memory. However, dynamic links are superior to static links. In some cases, dynamic links may cause some performance damage.


After the above five processes, the C ++ source program is eventually converted into an executable file. By default, the executable file is named a. out.

 

 

 

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.