【iOS】—— compile link

【iOS】—— compile link

compilation process

The compilation process is divided into four steps

  • Preprocessing
  • Compilation
  • Assembly
  • Linking

insert image description here

Preprocessing (Precompiled Prepressing)

As the first step of compiling, first main.mcompile the file into main.ia file, the instructions are as follows:

clang -E main.m -o main.i

Although the preprocessing process is to compile the main.m file into a mian.i file, the actual process is not so simple, so what is the specific precompilation situation: precompilation is to process the source
code All precompiled directives starting with #. The rules are as follows:

  • #defineDelete and expand the corresponding macro definition.
  • Process all conditional precompile directives. Such as #if, #ifdef, #else, #endif.
  • #include& #importIncluded files are recursively inserted here (including #).
  • Remove all comments //, /**/, etc.
  • Add line number and filename identifiers. Such as # 1 "main.m" (will be used for compilation and debugging).

Compilation

To main.icompile the file into main.sa file, the command is as follows:

clang -S main.i -o main.s

This process is to carry out the above main.i file: lexical analysis, syntax analysis, static analysis, optimize and generate corresponding assembly code, and finally generate main.sthe file.

Here we need to understand these terms:

  • Lexical analysis: Divide the character sequence of the source code into one by one token(keywords, symbols, literals, special symbols), such as putting identifiers into the symbol table.
  • Syntax analysis: Generate the abstract syntax tree AST, at this time the priority of the operation symbols is determined; some symbols have multiple meanings are also determined, for example: * is a multiplication sign or the content of the pointer; , etc., will report an 表达式不合法error 括号不匹配.
  • Static Analysis: Analysis 类型声明and 匹配问题. For example, adding an integer and a string will definitely report an error.
  • Intermediate grammar generation: Translate step by step CodeGenfrom top to bottom , and optimize expressions that can be determined at compile time , such as a=1+3 in the code, which can be optimized to a=4. (if bitcode is enabled)AST(抽象语法树)LLVM IR
  • Object code generation and optimization: generate machine-dependent assembly language based on intermediate syntax; and optimize assembly language. In this process, if there is a variable and it is defined in the same compilation unit, then allocate space for the variable and determine the address of the variable. If the variable or function is not defined in this compilation unit, the address cannot be determined until link time.

Assembly

Compile main.sthe file into main.oa file (that is, the object file we often say), the instructions are as follows:

clang -c main.s -o main.o

This process is to translate the assembly instructionsmain.s in the file obtained above into machine instructions , and finally generate and wait untilmain.o

Linking

This process is to main.ocompile it into a corresponding Mach-Ofile, which is what we often call an executable file. The instructions are as follows:

clang main.o -o main

The essence of linking is to combine one or more object files and required libraries (static library/dynamic library, if necessary) into one file (Mach-O executable file)

insert image description here

Guess you like

Origin blog.csdn.net/m0_62386635/article/details/131720416