C language compilation process

Source: Internet
Author: User

Compile and compile Program Reads the source program (NLP stream), analyzes the lexical and syntax, converts advanced language commands into functional equivalent Assembler Code Then, the assembler is converted to the machine language, and the executable program is generated according to the requirements of the operating system for the executable file format.

C source program header file --> pre-compiled processing (CPP) --> compiled program itself --> Optimized Program --> assembler --> linked program --> Executable File

1. Compile preprocessing

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

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


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 pre-compiled 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 phase


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. The pre-compiled program performs lexical analysis and syntax analysis to translate all commands into equivalent intermediate code representation or assembly code after confirming that they comply with the syntax rules.

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.


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


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.


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.


(2) In this way, the code of a function is put in a target file called a dynamic link library or shared object. 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.


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.




Makefile Compilation

Makefile is used for automatic compilation and linking. A project consists of many files. Changes to each file will lead to re-linking of the Project -----
However, not all files need to be re-compiled. makefile can record the file information and decide which files need to be re-compiled during the link!

In Unix systems, makefile is used with the make command.
For example, I have main. c. Window. c. model. c. Data. C 4. c file and window. h. model. h. Data. H 3. h file.
Main. c is the main program, which contains the main () function. All others are modules.

To generate the final executable file, follow these steps:
1. Compile Windows respectively. c. model. c. Data. c. Main. c. You will get three target files: window. o, model. o, Data. o, Main. O
2. link these four. O files to main.out(main.exe in windows ).

These files have a logical relationship, otherwise the compiler does not know how to compile them.

ALL: Main. Out
Main. Out: Main. O window. O model. O data. o
Gcc-O main. out main. O window. O model. O data. o

# The above means:
# All: Main. Out
If you want to compile all: Make all, the main. out executable file will be generated.

# Main. Out: Main. O window. O model. O data. o
To generate this main. Out, you need to rely on the main. O, window. O, model. O, and data. O files.

# Gcc-O main. out main. O window. O model. O data. o
This statement calls the compiler compilation, and VC uses Cl. When mutating, you can add many parameters, defined macros, and link library paths.

Of course, it's not over yet. How do these. O dependent main. Out come from?

Window. O: window. c window. h
Gcc-C window. c

Model. O: model. C model. h
Gcc-C model. c

Data. O: Data. C data. h
Gcc-C data. c

The above-C parameter specifies that the compiler can compile a. o file. Do not look for the main () function for link work.

Together, these are makefiles. Of course, these functions are too few and many other projects can be added. But its purpose is:
Let the compiler know which files are dependent on to compile a file. When the dependent files have changed, the compiler will automatically find that the final generated file is out of date,
And re-compile the corresponding module.

The current VC ++ is so good that you don't need to input makefile in one word.

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.