Like many programmers, the compiler has left me confused so far. For example, the relationship between the compiler and the operating system, the relationship between the compiler and the CPU, and where the dynamic linker looks for shared libraries. There are several confusing reasons: the first is the special functional role of the compiler, the compiler is the program that generates the program, and the second is the compilation process becoming more and more complex [note], A compiler supports multiple programming languages, shared libraries, compilation optimization, and compilation and link separation. Third, the intervention of the operating system. This article tries to take a look at the impact of the operating system on the compiler after the compilation process to see what the operating system has to do with the compiler.
Note: the complexity of the compilation process comes from the complexity of computer applications. For example, application projects become larger and larger, and more functions are available. To manage large projects, multiple source program files are split. To improve program performance, the link period of target program files is delayed until running; various script tools, such as make and configure (Shell), are introduced for flexible installation and upgrade of programs ).
Compile the program
Let's look at a traditional definition of the compiler:
Compile a program is a program that converts a program with a high degree of abstraction (also called the source program) into a programming language program with a low degree of abstraction (also called the Target Program) [Program] [note]. The two ends of abstraction are machine semantics and human understanding semantics. The [processor system] and [programming language] are the fundamental attributes of a compilation program.
Note: The following uses only the term "program". "tool" is an image metaphor and is not rigorous enough. Software is a product program, and it is best to use it only in the business context.
In the modern sense, the above definition of the compiled program is not enough to fully understand the compiled program, because it does not involve the operating system or the modern complex program building process. Programmers with certain development experience know that the program's "compilation process" includes compilation, Link (static link and dynamic link), debugging, and configuration and installation. The word "compile" cannot describe this process very well. The whole process can be called "program building", but compilation is only the first step. At this point, the Compilation Program is different in the traditional sense and in the modern sense. This article attempts to discuss the relationship between the Compilation Program and the operating system. To avoid ambiguity, the [Compilation Program] In this article includes two parts: Compilation and static link. The dynamic link part is somewhat special, the role will be mentioned later.
Next we will give three questions about the relationship between the operating system and the compiler, and try to answer them:
- First, what is the relationship between the Compilation Program and the operating system?
- Second, is the Compilation Program dependent on the operating system?
- Third, what is the relationship between the Compilation Program and the CPU?
- Fourth, what is the impact of the operating system on the relationship between the c Standard Library and the C Compilation Program?
In order to better discuss the next step, I first give a complete definition of the modern Compilation Program, and the problem is raised by the definition:
Modern Compiling Program is a program that converts a program with a higher degree of abstraction to a program with a lower degree of abstraction running under a hardware and software system ]. The so-called hardware system refers to the processor system, and the software system refers to the operating system.
To answer the first three questions, we need to clarify the concept of a program in the definition of modern compiled programs, and have a deeper understanding of the operating system.
Program
Programs are classified in a variety of ways. For example, the common division is system programs and applications ]. This is a coarse-grained method based on different computing tasks. Let's look at the definition of the program:
A Program (Program) is a [command] sequence used to complete a specific [computing task]. commands are read and operated by the corresponding [Turing Machine.
According to the above definition of the program, you can further classify the program according to the two standards: The [nature of commands] of the program and the [nature of Turing machines] of the read program. For example, the sequence of commands can be divided into independent programs and shared programs (using a shared library); the abstract degree of commands can be divided into high-level and low-level language programs. According to the [Turing Machine] system can be divided into x86 program and arm program, 16-bit program and 32-bit program.
Is an advanced language program a [Program ]? If so, what is its [Turing Machine?
Generally, the program refers to a binary executable file. Is the source program of an advanced language a program? From the definition of instruction sequence, [source program of advanced language] is a program, because the same [source program of advanced language] and [executable file of binary] are also a sequence of instructions, however, the [Turing Machine] of the [source program of advanced language] is not a CPU, nor a compiler or interpreter, but a programmer. [Source program of advanced language] is more reflected in the mutual learning and communication between programmers.
In addition to the above basic categories, modern programs will also be affected by the operating system that provides virtual runtime environments for them. programs can be further classified based on the operating system's system attributes, such as Win32 programs, linux program.
What kind of programs is the operating system?
The operating system is a relatively independent system program. The operating system supports a variety of system types such as 16-bit dos, 32-bit windows, x86 BSD, and ARM Linux. System programs generally refer to a program that directly provides semi-finished products (Virtual Environment for execution of applications) for applications and coordinates the Parallel Running of multiple applications. The so-called semi-finished product refers to a part of the system program (command sequence) is also part of the application (command sequence), but this part of the program is not exclusive to any application, it is shared. For example, drivers of various new hardware, C standard library functions, POSIX library functions, etc. As a Coordination Program, the operating system shows the procedural nature of general applications, such as independent scheduling threads, But they run in a State with higher power. Coordinating programs such as thread scheduling programs.
Relationship between non-operating system programs and Operating Systems
The operating system here refers to a modern 32-bit operating system like Linux, and the [non-operating system program] runs on the operating system and may depend on the operating system.
In fact, as long as the program runs on an operating system, the printing of the operating system will be branded and dependent on the operating system, including compiling programs. However, these programs depend on the operating system and the dependent content. For example, the simplest [Hello World Program] will depend on the C library of the operating system. If the input and output functions of [Hello World Program] are removed, for addition, subtraction, and logical operations only, the hello World Program still has a small amount of dependencies on the operating system, because the [Hello World Program] is compiled by the compilation program running on the [Operating System] and has a specific target file format, and the [loader of the Operating System] loads the memory to run [note]. This kind of "useless" program [in Form] relies on OS is the most operating system independent program. On this basis, other programs depend on the operating system to varying degrees. dependencies are manifested in dependencies on various libraries in the operating system, such as the C standard library and POSIX system library, the thread library, network library, and other third-party application code libraries based on these basic libraries.
Note: The Compilation Program, like the Bootstrap program and shell program, is the basic part of the modern operating system.
The problem is solved.
With the above understanding of the program and the nature of the operating system, we know that the Compiling Program has a certain affinity with the operating system. However, this kind of affinity may be confusing. For example, the Linux release version does not have a compilation program installed. Only the development workstation needs to compile the program. All Linux distributions may use shared libraries, and these shared libraries need to be dynamically linked. It can be seen that the relationship between the Compilation Program and the operating system should be viewed separately in the [development period] and [runtime period. During the development period, the compiling program runs on the operating system and belongs to the non-operating system program. It depends on the operating system. During the runtime, the sub-part of the compiled program-Dynamic Linking program and loading program are an integral part of the operating system.
The relationships between the compiled program and the operating system are as follows:
- First, the Compilation part and static link part of the compiled program are system programs running on the operating system;
- Second, the dynamic link part of the Compilation Program is more relevant to the operating system, so the dynamic link part can be completely independent [fixme: the relationship between the dynamic link program and the operating system is unknown];
- Third, the compilation output format of the Compilation Program is related to the operating system.
It can be seen that the Compilation Program is related to the operating system, and the Compilation Program is also an important part of the operating system functions, but the Compilation Program is not integrated into the operating system kernel, therefore, compiling programs is not an integral part of the operating system.
Compile program dependencies on the Operating System
As shown in the preceding figure, the compiled program runs the [non-operating system program] on the operating system and depends on the operating system. A Compilation Program is a program with a larger computing set. It is less dependent on the OS than the application. The dependencies include:
- Form dependency (compiled by another compiler with the same hardware and software system [local language compiler], or compiled by different software and hardware systems [Line-based cross-compiler], cross-compilation)
- C library dependency, read source code files of advanced languages, and write the target files of low-level languages
Relationship between compiled programs and CPU
This problem has been answered in the definition of the Compilation Program. A Compilation Program only compiles one machine code. We say that the [operating system relevance] of the compiled program is developed the day after tomorrow, and the [processor relevance] of the compiled program is born.
Impact of operating system on the relationship between C standard library and C compiled program
The operating system has no impact on the C library. The C library is a common code library, but with [fixme]