How does the compiler work? Detailed explanation of the working process of the compiler

Compiler introduction

Simply put, a compiler is a program that translates "one language (usually a high-level language)" into "another language (usually a low-level language)." The main workflow of a modern compiler: source code → preprocessor → compiler → object code → linker → executables

Type of compiler

The compiler can generate object code that runs in the same environment as the computer and operating system (platform) on which the compiler itself is located. This compiler is also called a "local" compiler. In addition, the compiler can also generate object code for running on other platforms. This compiler is also called a cross compiler. Cross compilers are very useful when building new hardware platforms. "Source to source compiler" refers to a compiler that uses a high-level language as input and a high-level language. For example: Automated parallelization compilers often use a high-level language as input, convert the code, and annotate it with parallel code comments (such as OpenMP) or with language constructs (such as FORTRAN's DOALL instruction).

How the compiler works

Compilation is a translation process from source code (usually a high-level language) to object code (usually low-level or machine language) that can be executed directly by a computer or virtual machine. However, there are also compilers ranging from low-level languages ​​to high-level languages, which are also called decompilers for regenerating high-level language code from low-level language code generated by high-level languages. There are also compilers that generate another high-level language from one high-level language, or a compiler (also called cascading) that generates intermediate code that needs further processing.

A typical compiler output is an object file consisting of the name and address of the entry point, and the machine code that is externally called (to a function call that is not in the object file). A set of object files, not necessarily the same compiler, but the compiler used must use the same output format, can be linked together and generate an EXE that can be directly executed by the user.

So the files on our computer are all compiled files.

Compiler working process

To run the source code, you must first convert it to a binary machine code, which is the task of the compiler. For example, the following source code (assuming the file name is called test.c).

#include "stdio.h"

Int main(void)

{

Fputs("Hello, world!", stdout);

Return 0;

}

It must be processed by the compiler before it can run.

$ gcc test.c

$ . /a.out

Hello, world!

For complex projects, the compilation process must also be divided into three steps.

$ . /configure

$ make

$ make install

This article will introduce the working process of the compiler, which is the task of each of the above three commands. I mainly refer to Alex Smith's article "Building C Projects". It should be stated that this article is mainly for the gcc compiler, that is, for C and C++, not necessarily for compiling in other languages.

How does the compiler work? Detailed explanation of the working process of the compiler

First step configuration (configure)

Before the compiler starts working, you need to know the current system environment, such as where the standard library is, where the software is installed, which components need to be installed, and so on. This is because the system environment of different computers is different. By specifying the compilation parameters, the compiler can flexibly adapt to the environment and compile machine code that can run in various environments. This step of determining the compilation parameters is called "configure".

These configuration information is stored in a configuration file, which is a script file called configure. Usually it is generated by the autoconf tool. The compiler knows the compilation parameters by running this script.

The configure script has taken into account differences in different systems and gives default values ​​for various compilation parameters. If the user's system environment is special, or there are some specific requirements, you need to manually provide compilation parameters to the configure script.

$ . /configure --prefix=/

How does the compiler work? Detailed explanation of the working process of the compiler

The above code is a compilation configuration of the PHP source code. The user specifies the installed file to be saved in the www directory, and the support of the mysql module is added at compile time.

Shark Antenna

shark fin antenna,car shark antenna,shark antenna car

Yetnorson Antenna Co., Ltd. , https://www.xhlantenna.com