C, C ++ language compilation process summary

Source: Internet
Author: User
Tags processing instruction

In C language [Yu Yan], the compilation of [Bian Yi] process is summarized in detail (for conversion). In this case, we intend to summarize the content of [Bian Yi, I accidentally saw a netizen write a very comprehensive article, so I made a little effort to make a memo.

 

The C language [Yu Yan] compilation [Bian Yi] links to [Lian Jie] to compile a C program (source code [Dai Ma] [Yuan Dai Ma]). convert [Zhuan Huan] to a program that can run [Yun hang] on the hardware [Ying Jian] (the [Zhi hang] Code [Dai Ma] can be executed), you need to compile [Bian Yi] and link [Lian Jie]. Compiling [Bian Yi] is to translate the source code [Dai Ma] [Yuan Dai Ma] of the text [Wen Ben] form into the machine language [Yu Yan] [Ji Qi Yu Yan] form. [mu Biao] File [Wen Jian] process. The link [Lian Jie] is to start the [Qi Dong] Code [Dai Ma] of the [mu Biao] File [Wen Jian], the operating system [XI Tong] [CaO Zuo Xi Tong]] and the library file [Wen Jian] used to organize and form the final process of generating the executable [Zhi hang] Code [Dai Ma. The process is illustrated as follows:

 

 

As shown in the figure, the [Bian Yi] compilation process of the entire code [Dai Ma] is divided into two processes: compiling [Bian Yi] and linking [Lian Jie, compile the [Bian Yi] section enclosed by braces in the corresponding graph, and the rest is the link [Lian Jie] process.

 

[Bian Yi] compilation process

The [Bian Yi] compilation process can be divided into two phases: compiling [Bian Yi] and compiling [Hui Bian].

 

Compile [Bian Yi]

Compile [Bian Yi] to read the [Du Qu] source program [Yuan Cheng Xu] (character [Zi Fu] stream ), perform lexical [ci fa] and syntax [Yu fa] analysis, convert the [Yu Yan] [Gao Ji Yu Yan] command [Zhi Ling] to [Zhuan Huan] for the compilation of [Hui Bian] Code [Dai Ma], the compilation of the source file [Wen Jian] [Bian Yi] contains two main stages:

 

The first phase is the pre-processing phase, which is carried out before the official compilation of the [Bian Yi] phase. The pre-processing phase will modify the content of the [Xiu Gai] source file [Wen Jian] based on the pre-processing instruction [Zhi Ling] that has been placed in the [Wen Jian] file. For example, the # include command [Zhi Ling] is a preprocessing Command [Zhi Ling], which adds the content of the header file [Wen Jian] To the. cpp file [Wen Jian. This method of modifying the [Xiu Gai] source file [Wen Jian] Before compiling [Bian Yi] provides great flexibility [Ling Huo Xing], to adapt to the [XI Tong] [CaO Zuo Xi Tong] environment [XI Tong Huan Jing] restrictions of different computers and operating systems. The Code [Dai Ma] required by one environment may be different from the code [Dai Ma] required by another environment, because the available hardware [Ying Jian] or the operating system [XI Tong] [CaO Zuo Xi Tong] is different. In many cases [Qing Kuang], [Dai Ma] codes for [Yong Yu] different environments can be placed in the same file [Wen Jian, then, modify the [Xiu Gai] Code [Dai Ma] In the preprocessing phase to adapt it to the current environment.

It mainly deals with the following aspects:

 

(1) macro definition Command [Zhi Ling], for example, # define a B
For this pseudo Command [Zhi Ling] [Wei Zhi Ling], to pre-compile [Bian Yi] [Yu Bian Yi], replace [Ti Huan] with B for all a in the program. however, [Ti Huan] is not replaced with a of the [Zi Fu] string [Zi Fu Chuan] constant [chang Liang]. In addition, # UNDEF will cancel [Qu Xiao]'s definition of a macro so that the appearance of this string will not be replaced by [Ti Huan].

 

(2) condition [Tiao Jian] compile the [Bian Yi] command [Zhi Ling], for example, # ifdef, # ifndef, # else, # Elif, # endif.
The introduction of these pseudo commands [Zhi Ling] [Wei Zhi Ling] allows programmers [Cheng Xu yuan] to determine how to compile the [Bian Yi] program [Bian Yi Cheng Xu] which codes are processed by [Dai Ma. Pre-compile the [Bian Yi] [Yu Bian Yi] program [Bian Yi Cheng Xu] [Yu Bian Yi Cheng Xu] based on the relevant files [Wen Jian], filter out unnecessary code [Dai Ma] [Guo LV.

(3) the header file [Wen Jian] contains the command [Zhi Ling], such as # include "FILENAME" or # include <FILENAME>.

In the header file [Wen Jian], the pseudo Command [Zhi Ling] [Wei Zhi Ling] # define defines a large number of macros (the most common character is the [Zi Fu] constant [chang Liang ]), it also contains the declaration of various external symbols [Sheng Ming]. The purpose of using the header file [Wen Jian] is to make some definitions available for multiple different C source programs [Yuan Cheng Xu. Because in the C source program [Yuan Cheng Xu] That needs to use these definitions, you only need to add the # include statement [Yu Ju, instead of repeating these definitions in this file [Wen Jian. Pre-compiled [Bian Yi] [Yu Bian Yi] program [Bian Yi Cheng Xu] [Yu Bian Yi Cheng Xu] adds all the definitions in the header file [Wen Jian] to output [Shu Chu] File [Wen Jian, for compilation of [Bian Yi] program [Bian Yi Cheng Xu. The header file [Wen Jian] contained in the C source [Yuan Cheng Xu] can be provided by the system [XI Tong, these header files [Wen Jian] 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 file [Wen Jian]. These files [Wen Jian] are generally placed in the same directory as the C source program [Yuan Cheng Xu, double quotation marks ("") are used in # include ("").

(4) special symbols, pre-compiled [Bian Yi] [Yu Bian Yi] program [Bian Yi Cheng Xu] [Yu Bian Yi Cheng Xu] can recognize some special symbols of [Shi BIE.
For example, the line Identification [Biao Shi] that appears in the source [Yuan Cheng Xu] will be interpreted as [Jie Shi] as the current row number (in decimal format [shi jin Zhi ), file is interpreted as the name of the C source program [Yuan Cheng Xu] currently compiled by [Bian Yi. Pre-compiled [Bian Yi] [Yu Bian Yi] program [Bian Yi Cheng Xu] [Yu Bian Yi Cheng Xu] replace [Ti Huan] with the appropriate value.

After pre-Compilation of the [Bian Yi] [Yu Bian Yi] program [Bian Yi Cheng Xu] [Yu Bian Yi Cheng Xu], basically the" replace "work. After this replacement, generate an output [Shu Chu] File [Wen Jian] with no macro definition, no condition [Tiao Jian] to compile the [Bian Yi] command [Zhi Ling], and no special symbols. The meaning of this file [Wen Jian] is the same as that of the source file [Wen Jian] without preprocessing, but the content is different. Next, this output [Shu Chu] File [Wen Jian] will be translated into machine commands [Zhi Ling] as the output [Shu Chu] of the [Bian Yi Cheng Xu] program.] [Ji Qi Zhi Ling].

In the second phase, compile [Bian Yi] and optimize [You Hua, only [chang Liang] is the constant in the output [Shu Chu] File [Wen Jian] obtained by pre-compiling [Bian Yi] [Yu Bian Yi]. for example, the number [Shu Zi], the character [Zi Fu] string [Zi Fu Chuan], and the variable [Bian Liang] are defined, and the keywords [Guan Jian Zi] of the C language [Yu Yan], such as main, if, else, for, while, {,}, +,-, *,/, and so on.

To compile the [Bian Yi] program [Bian Yi Cheng Xu], you must analyze [CI Fa Fen XI] and [Yu fa] Through lexical [ci fa ]. fen XI], after confirming that all [Zhi Ling] commands of [Que Ren] comply with the syntax [Yu fa] rules, translate it into equivalent intermediate code [Dai Ma] [Zhong Jian Dai Ma] to represent or compile [Hui Bian] Code [Dai Ma].

Optimization of [You Hua] processing is a difficult technology in compiling [XI Tong] [Bian Yi Xi Tong] system. [Wen ti] is related not only to the compilation of [Bian Yi] technology, but also to the hardware [Ying Jian] environment of the machine. Part of optimization [You Hua] is the optimization of the intermediate code [Dai Ma] [Zhong Jian Dai Ma] [You Hua]. This optimization [You Hua] does not depend on a specific computer. Another optimization [You Hua] is mainly used to generate the target [mu Biao] Code [Dai Ma.

For the previous optimization [You Hua], the main task is to delete the [Shan Chu] public expression [Biao Da shi], cycle [Xun Huan] to optimize [You Hua] (code [Dai Ma] out-of-context, and weaken the intensity of [Qiang du Xue RuO], changing [Bian Huan] loops [Xun Huan] to control [Kong Zhi] condition [Tiao Jian], merging of known quantities, etc), duplication propagation, and useless value assignment [Fu Zhi] to delete [Shan Chu.

The Optimization of the latter type [Lei Xing] [You Hua] is closely related to the hardware [Ying Jian] structure [Jie Gou] of the machine, the main consideration is how to make full use of the value of the variable [Bian Liang] stored in the [Ying Jian] register [Ji Cun Qi] of each hardware of the machine, to reduce the number of accesses to the memory [Nei Cun. In addition, how to execute the [Zhi hang] command [Zhi Ling] Based on Machine Hardware [Ying Jian] (for example, pipeline [Liu Shui Xian], Proteus, CISC, VLIW, etc) [Tiao Zheng] makes some adjustments to the Command [Zhi Ling] to make the target [mu Biao] Code [Dai Ma] relatively short, and the efficiency of executing [Zhi hang] is relatively high, it is also an important research topic.

Compile [Hui Bian]

Compilation [Hui Bian] actually refers to the compilation of [Hui Bian] language [Yu Yan] [Hui Bian Yu Yan] Code [Dai Ma] into the target [mu Biao] Machine [mu Biao ji] indicates the process of [Zhi Ling] [Ji Qi Zhi Ling] commands. For each C language [Yu Yan] source program [Yuan Cheng Xu] processed by the Translation System [XI Tong], the target [mu Biao] File [Wen Jian] will be obtained after this processing. What is stored in the [mu Biao] File [Wen Jian] is the machine language [Yu Yan] [Ji Qi Yu] of the target [mu Biao] equivalent to the source [Yuan Cheng Xu ]. yan] Code [Dai Ma]. The [mu Biao] File [Wen Jian] consists of segments. Generally, a target [mu Biao] File [Wen Jian] contains at least two segments:

 

Code [Dai Ma] section: This section mainly contains the program instructions [Zhi Ling]. This section is generally readable and executable [Zhi hang], but generally cannot be written.

Data [Shu Ju] section: Mainly stores various global variables used in the program [Bian Liang] [Quan Ju Bian Liang] or static data [Shu Ju]. Generally, the [Shu Ju] segment of the data is readable, writable, and can be executed in [Zhi hang.

 

In UNIX environments, there are three main types of [Lei Xing] target [mu Biao] File [Wen Jian]:

(1) relocated [Ding Wei] File [Wen Jian]
This includes the [mu Biao] File [Wen Jian] link [Lian Jie] suitable for other targets to create a [Zhi hang] executable or shared target [mu Biao] File [wen Jian] Code [Dai Ma] and data [Shu Ju].

(2) shared destination [mu Biao] File [Wen Jian]

This file [Wen Jian] stores the Code [Dai Ma] and data [Shu Ju] suitable for linking [Lian Jie] in two contexts [Shang Xia Wen]. The first method is to link the [Lian Jie] program to process it together with other [Ding Wei] File [Wen Jian] and the shared target [mu Biao] File [Wen Jian ]. to create another [mu Biao] File [Wen Jian]; the second type is dynamic [Dong Tai] link [Lian Jie] program to associate it with another executable [Zhi hang] File [Wen Jian] [ke Zhi hang Wen Jian] and other sharing the target [mu Biao] File [Wen Jian] is combined, create a process [Jin Cheng] image.

(3) executable [Zhi hang] File [Wen Jian] [ke Zhi hang Wen Jian]
It contains a file [Wen Jian] that can be used by the operating system [XI Tong] [CaO Zuo Xi Tong] to create a process [Jin Cheng] to execute [Zhi hang]. Compile the [Hui Bian] program [Hui Bian Cheng Xu] to generate the [mu Biao] File [Wen Jian] of the first [Lei Xing] type. For the latter two types, some other processing is required. This is the work of linking the [Lian Jie] program.

 

Link to the [Lian Jie] Process

The target [mu Biao] File [Wen Jian] generated by the [Hui Bian] program [Hui Bian Cheng Xu] cannot be executed immediately [Zhi hang], there may be many unsolved problems [wen ti].

For example, the function [Han Shu] In a source file [Wen Jian] may reference a symbol defined in [Yin Yong] in another source file [Wen Jian] (for example, the variable [Bian Liang] or the function [han Shu] calls [Tiao Yong] [Han Shu Tiao Yong] and so on ); in the program, the [Tiao Yong] function [Han Shu] In a library file [Wen Jian] may be called, and so on. All these problems [wen ti] can be solved only after being processed by the [Lian Jie] program.

The main task of linking the [Lian Jie] program is to connect the [mu Biao] File [Wen Jian] to each other [Lian Jie], the [Yin Yong] symbol will be referenced in a file [Wen Jian] To connect [Lian Jie] with the definition of this symbol in another file [Wen Jian, make all these target [mu Biao] files [Wen Jian] An operating system [XI Tong] [CaO Zuo Xi Tong] loaded into [Zhuang RU] And run [Zhi hang].

Based on the [Lian Jie] Link Method of the same library function [Han Shu] specified by the developer, the link [Lian Jie] processing can be divided into two types:

 

(1) Static link [Lian Jie]

In this connection [Lian Jie] mode, the Code [Dai Ma] of the function [Han Shu] will be copied from its static link [Lian Jie] Library [Kao bei] to the final executable [Zhi hang] program [zhi hang Cheng Xu] [ke Zhi hang Cheng Xu. In this way, when the program is executed [Zhi hang], the Code [Dai Ma] will be loaded into [Zhuang RU] to the virtual [Xu NI] address of the Process [Jin Cheng] [di zhi] [Xu Ni di Zhi] space [Kong Jian] [di Zhi Kong Jian. The static link [Lian Jie] library is actually a collection of the [mu Biao] File [Wen Jian, each file [Wen Jian] contains the Code [Dai Ma] of one or a group of related functions [Han Shu] in the library.

 

(2) Dynamic [Dong Tai] link [Lian Jie]

In this way, the Code [Dai Ma] of the function [Han Shu] is put in a dynamic [Dong Tai] link [Lian Jie] Library [Dong Tai lian jie Ku] or shared object [DUI Xiang]. in the [mu Biao] File [Wen Jian. Link to the [Lian Jie] program. At this time, only the shared object [DUI] is recorded in the final executable [Zhi hang] program [Zhi hang Cheng Xu] [ke Zhi hang Cheng Xu ]. xiang] Name and a small amount of registration information [Xin XI]. When the [Zhi hang] File [Wen Jian] [ke Zhi hang Wen Jian] is executed, dynamic [Dong Tai] link to [Lian Jie] Library [Dong Tai lian jie Ku] will be mapped to [Ying she] to [Yun hang Shi] when running [Yun hang] the virtual address of the corresponding process [Jin Cheng] [di Zhi] space [Kong Jian] [di Zhi Kong Jian]. The dynamic [Dong Tai] link to the [Lian Jie] program will be based on the information recorded in the executable [Zhi hang] program [Zhi hang Cheng Xu] [ke Zhi hang Cheng Xu] [Xin XI] Find the corresponding function [Han Shu] Code [Dai Ma].

For the function [Han Shu] In the executable [Zhi hang] File [Wen Jian] [ke Zhi hang Wen Jian], call [Tiao Yong] [Han Shu Tiao Yong], you can use the dynamic [Dong Tai] link [Lian Jie] or static link [Lian Jie] method [Fang fa]. Using Dynamic [Dong Tai] to link [Lian Jie] makes the final executable [Zhi hang] File [Wen Jian] [ke Zhi hang Wen Jian] short, in addition, when the shared object [DUI Xiang] is used by multiple processes [Jin Cheng], it can save some memory [Nei Cun]. because in the memory [Nei Cun], you only need to save the [Dai Ma] Code of the shared object [DUI Xiang]. However, instead of using dynamic [Dong Tai] to link [Lian Jie], it must be superior to using static link [Lian Jie. In some cases, the dynamic [Dong Tai] link [Lian Jie] in [Qing Kuang] may cause some performance [Xing Neng] damages.

The GCC compiler [Bian Yi] [Bian Yi Qi] used in Linux binds the above processes, [Yong HU] can use the [Ming Ling] command only once to compile [Bian Yi], which makes it easier to compile [Bian Yi, however, it is very unfavorable for beginners to understand the compilation process of [Bian Yi], that is, the compilation process of [Bian Yi] by the GCC agent:

 

 

We can see that:

 

Pre-compile [Bian Yi] [Yu Bian Yi]
Convert the. c file [Wen Jian] To the. I file [Wen Jian]
The GCC Command [Ming Ling] used is: gcc-e
Corresponding to the pre-processing command [Ming Ling] CPP

 

Compile [Bian Yi]
Convert the. c/. h file [Wen Jian] into the. s file [Wen Jian]
The GCC Command [Ming Ling] used is: gcc-S
Corresponding to compiling [Bian Yi] command [Ming Ling] CC-S

 

Compile [Hui Bian]
Convert the. s file [Wen Jian] To the. o file [Wen Jian]
The GCC Command [Ming Ling] used is: gcc-C
The [Ming Ling] command for compiling [Hui Bian] is

 

Link [Lian Jie]
Convert the. o file [Wen Jian] to an executable [Zhi hang] program [Zhi hang Cheng Xu] [ke Zhi hang Cheng Xu]
The GCC Command [Ming Ling] used is: gcc
The connection [Lian Jie] command [Ming Ling] Is LD

 

To sum up, the compilation of [Bian Yi] involves the above four processes: pre-compile [Bian Yi] [Yu Bian Yi], compile [Bian Yi], compile [Hui Bian], and link [Lian Jie]. Lia understands the work in these four processes and helps us understand the working processes of header files [Wen Jian] and libraries, in addition, the [Bian Yi] link [Lian Jie] process is clearly understood. We also locate the [Ding Wei] error [CuO Wu] when programming [Bian Cheng]. when programming [Bian Cheng], it is very helpful to mobilize and compile [Bian Yi] [Bian Yi Qi] to detect [CuO Wu] errors.

  • 0
  • Top
Related Article

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.