Introduction to the C + + compilation process

Source: Internet
Author: User

  • Understand the benefits of the compilation process
      • C + + Engineering-related issues
        • What is a library? What's the difference between a static library and a dynamic library?
        • What role does the header file play?
  • Introduction to the compilation process
    • Noun:
      • Compile: Translate source code into machine language and save it to target file. If the compilation passes, the CPP is converted into an obj file.
      • Compilation Unit:
          • Each CPP is a compilation unit, and each compilation unit is independent and unknown to each other. A compilation unit (translation Unit) refers to a. cpp file and all the. h files in this include, the code inside the. h file is extended to the. cpp file containing it, and the compiler compiles the. cpp file as an. obj file, which has a PE (portable executable , which is the Windows executable file format, and itself contains binary code, but not necessarily executable, because there is no guarantee that there must be a main function. When the compiler compiles all the. cpp files in a project in a separate way, it is linked by the linker to an. exe or. dll file.
      • Target files: Compiled files, in the form of machine code contains all the functions and data in the compilation unit, export symbol table, unresolved symbol table, address redirect table , etc.
          • Type of destination file:
            • relocatable files (. o,. obj files): It contains code and data that are appropriate for other target file links to create an executable or shared target file. Each CPP is compiled into an. o file
            • Shared destination file (library file)
              • This file holds code and data that are appropriate for linking in both contexts.
                • The first is that the linker (static library) can process it with other relocatable files and shared target files to create another target file
                  • A static link library is actually a collection of target files in which each file contains code for one or a set of related functions in the library
                • The second is a dynamic-link program (a dynamic library) that adds it to another execute files and other shared target files together to create a process image
                  • dynamic-link libraries are called only when the program executes
            • Executable file
              • A file that can be executed by the operating system to create a process
          • . o files are available after compilation, but library files and executables need to be linked before they can be obtained
    • C + + program compilation process diagram
    • Compilation process
      • Role: compile is to read the source program (character stream), the lexical and grammatical analysis, the high-level language instruction into the functional equivalent assembly code, and then converted to machine code, generate the target file (. obj)
      • Divided into two processes
          • Compile:
            • preprocessing phase
              • Macro # define
              • conditional compilation directives, such as #ifdef, #ifndef, #else, #elif, #endif and so on.
              • Header file contains, #include <iostream>
              • Special symbols
                • Line logo will be interpreted as the current row number (decimal number)
                • File is interpreted as the name of the currently compiled C source program. The precompiled program will replace the strings that appear in the source program with the appropriate values
            • Compile, optimize phase
              • For code optimization, do not rely on specific computers
              • Optimized for computers
          • Assembly
            • The assembly language code is translated into the target machine instruction, and the target file (. o file,. obj file) is generated. This process relies on the hardware and operating system environment of the machine.
      • 3 tables:. o files must provide at least 3 sheets
          • Export symbol table: the symbol and address that the target file can provide
          • Unresolved symbol table: A list of symbols that cannot be found, telling the linker that the symbols did not find the address
          • Address Redirection table:
            • When linking, the linker will find the address in other target files for the symbol in the "unresolved symbol table" of the target file, but the address of each target file is starting from 0x0000, so it is obvious that the address of the symbol in the file is not correct, in order to distinguish the different files, The linker adjusts the address of each destination file as it is linked. In this case, If B.obj's 0x0000 is located on the 0x00001000 of the executable, and A.obj's 0x0000 is located on the 0x00002000 of the executable, then the a.obj's exported symbolic address will be added to all the 0x00002000,b.obj symbols on the linker. The address will also be added 0x00001000. This ensures that the addresses are not duplicated.
            • Because the starting address is added, the actual address of the symbol in its own file is not correct, and you need to use an address redirection table to record the symbol relative to its own file address
      • Example:
    • Link process
      • Link: The main task of the linker is to connect the relevant target files (library files,. o files) to each other, and the symbols referenced in one file are connected to the definition of the symbol in another file , So that all these target files become a system can be loaded into the implementation of the whole.
      • Specific work:
        When the linker makes a link, it first determines the location of each target file in the final executable file. The address redefinition table for all destination files is then redirected (plus an offset, that is, the starting address of the compilation unit on the executable file). It then iterates through the unresolved symbol tables of all the target files and finds the matching symbols in all the exported symbols, and fills in the implementation address in the position recorded in the unresolved symbol table. Finally, the contents of all the target files are written in their respective positions, and then some other work is done to generate an executable file.
      • How to link
          • Static Link: The code of the function will be copied from its location in the static link library to the final executable program. The code is then loaded into the virtual address space of the process when it is executed.
          • Dynamic linking: The code of a function is placed in a target file called a dynamic link library or a shared object. What the linker does at this point 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 is executed, the entire contents of the dynamic-link library are mapped to the virtual address space of the corresponding process at run time . The dynamic linker will find the appropriate function code based on the information recorded in the executable program .
    • Comparison of the two ways of linking
  • Some of the features available in C/
      • extern: This is to tell the compiler that this variable or function is defined in another compilation unit, that is, to place the symbol in the unresolved symbol table (external link).
      • Static: If the keyword is in front of a global function or variable declaration, it indicates that the compilation unit does not export the function or variable, because some of this symbol cannot be used in another compilation unit (internal link). If it is a static local variable, the variable is stored in the same way as a global variable, but the symbol is still not exported.
      • Default Link properties: For functions and variables, the default link is an external link, and for a const variable, the default internal link.
      • The pros and cons of external links: externally linked symbols can be used throughout the program, which requires other compilation units not to export the same symbol (otherwise it will be reported duplicated external symbols).
      • Why the header file can generally only have a declaration cannot have a definition: the header file can be included in multiple compilation units, if there is a definition in the header file, then each containing the header file of the compilation unit will be the same symbol is defined, if the symbol is an external link, it will cause duplicated external Symbols link error.
      • Why is the common use of the inline function defined in the header file: Because compile-time between the compilation unit is not known, If the inline is defined in a. cpp file, there is no way to find the definition of the function when compiling other compilation units that use the function, because the function cannot be expanded (the inline function does not expand, that is, instead of tagging the function code at the point of use and jumping again, the code is embedded directly). So if the inline function is defined in. cpp, then only this. cpp file can use it.
      • The inline function in. h can be contained by more than one CPP without causing a sign conflict, because it is embedded directly into the call where the inner join does not form an external symbol and is not visible externally
  • Common compilers
  • Makefile and make tools
  • Common compiler usage methods
  • Compilation Error Resolution


From for notes (Wiz)

Introduction to the C + + compilation process

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.