Summary of basic principles of iOS

The purpose of this article is to analyze the loading process of dyLD and understand what else is done at the bottom before main function

primers

  • Create a project, overwrite the load method in ViewController, add a C++ method in main, kcFUnc, what is their print order?

  • Load -> kcFunc -> main -> main

Why is it in this order? Isn’t main an entry function? Why wasn’t Main executed first?

Following this question, let’s explore what else we did before we went to main.

Compilation process and libraries

Before analyzing app startup, we need to understand the compilation process of iOSapp code and dynamic and static libraries.

The build process

The compilation process is shown in the figure below, which is mainly divided into the following steps:

  • The source fileH,. M, and. CPP files are loaded
  • pretreatment: Replaces macros, deletes comments, expands header files, and generates.i files
  • compile: Converts the. I file to assembly language to generate. S file
  • assembly: Converts assembly files to machine code files to generate. O files
  • link: References other libraries in the.o file to generate the final executable