We know that from the middle of the program you wrote to the build executable, the F7 command was executed under VS, the Makefile was executed under Linux, or a G++/GCC xxx-o was written yourself yyy So this process includes: C source program header file----pre-compile processing (CPP)----the compiler itself-----optimizer----the assembler----The executable file 1. Compile pre-processing : The precompiled program is basically the replacement of the source program. After this substitution, generate a no macro definition. No conditional compilation directives, no special symbols for output files. (A.) Macro Definition Directives : #define NUMBER INT---note no semicolon #undef number---> Cancel the substitution of the defined int (b.) Conditional compilation Directives : #ifdef, #ifndef, #else, #elif, #endif---> Decide which code the compiler will handle by defining different macros. The precompiled program will filter out unnecessary code according to the relevant files. (c.) Header file contains Directives : #include <>--> library header files in the/usr/include directory (d.) Special characters : The precompiled program can recognize some special symbols. For example, the line ID in the source program will be interpreted as the current row number. File is interpreted as the name of the current compiled C source program 2. Compiling The job of precompiling the program at this stage is to translate it into equivalent intermediate code or assembly code by lexical analysis and parsing, after confirming that all instructions conform to the grammatical rules 3. Optimization Optimizing processing is a difficult technique in a compiled system. It involves two aspects: the compilation technology itself (software optimization) \ Machine hardware environment (hardware-related optimizations) Software optimization: Delete common expressions, loop optimization (out-of-code, strength weakening, transform loop control conditions, known amounts of merging), replication propagation, deletion of useless assignments Hardware-related optimizations: How to make full use of the various variables stored in the machine's various hardware registers to reduce the number of accesses to memory. According to the characteristics of the machine hardware pointing instructions (such as pipelining, RISC,CISC,VLIW) The instructions make some adjustments so that the target code is shorter and the execution is relatively efficient. The optimized assembly code must be translated into the appropriate machine instructions by the assembler assembly, which may be executed by the machine 4. Compilation Assembly process time is the process of translating assembly language code into target machine instructions . The machine instruction file is the machine language code also called the target file . In general, the target file consists of segments. There are usually at least two segments on the target file: Code Snippet: This paragraph contains mainly the instructions of the program. The paragraph is generally readable and executable, but is generally not writable Data segment: a variety of global variables or static data to be used in the main storage program. The general data is readable and writable. There are three main types of target files in the UNIX environment: A. relocatable file : Contains code and data for linking to other target files to create an executable or shared target file B. Shared target files : This file holds code and data that are appropriate for linking in both contexts: the first is that the linker can process it with other relocatable files and shared files to create another target file; The second is that the dynamic linker combines it with another executable file and other shared target files to create a process image c. executable file : It contains a file that can be executed by the OS to create a process . The assembler generates a target file that is actually the first type (relocatable file). For the latter two other processes are also needed to get the This is the work of the link program. 5. Links The target file generated by the assembler is not immediately executed, and there may be many unresolved issues: for example, a function inside a a.cpp refers to a symbol (such as a variable or function) defined inside a b.cpp; For example, a function that calls a library file in a program. These problems need to be handled by the linker. And the way to do this is to connect the associated target file ( For example, the target file of the a.cpp above and the target file of the b.cpp. or a.cpp target file and the library's target file. In other words, the symbol referenced in one file is connected to the definition of the symbol in another file, Make all of these target files a unified whole that can be loaded into the operating system. Depending on how the developer assigns the same library functions , link processing can be divided into two types: 1. Static link In this way, the code of the function is copied from the static link library in the location of the function to the final executable program . The process is that the code of the function is loaded into the executable process (which is also the process of the function code) when it is executed. Executable program and function code in a process) in the virtual address space (that is, copied into the executable program). A static link library is actually a collection of target files , each of which contains code for one or a set of related functions in the library . 2. Dynamic linking In this way, the code of the function is placed in a place called a dynamic link library or in a target file of a shared object . At this point the linker does its work in the final The name of the shared object and other minor and mandatory registration information are recorded in the executable program. The entire contents of the dynamic-link library are mapped to the virtual address space of the corresponding process at run time when it is executed. The dynamic link library will Find the appropriate function code based on the information that is logged in the executable program. For function calls in executables, dynamic linking and static linking are all possible . Using dynamic links can make the final executable file shorter and save some memory if the shared object is used by more than one process (only one copy of the code for this shared object is stored in memory), because the function code is not put into the executable. Instead, it maps to a virtual address space where the executable file accesses the function code based on the clues it holds about the code of the function to the virtual address. But this This is not to say that dynamic links are better than static links , because in some cases the performance of static links is higher 6. executable file. Article Learning Source http://blog.csdn.net/zhongguoren666/article/details/6672988 |