1. Probe into the library format 2. Probe into the framework format, what is Embed 3. 4. Dyld explores the dynamic library search process and @Roath analysis 5. 6. Static and dynamic library linksCopy the code
0. How to check whether the library is static or dynamic
  • (1) CD xx.framework
  • (2) file xx
  • (3) Judge: The static library contains “Current AR Archive Random Library”. 4) Dynamically Linked shared Library
  • (4) Check the instruction set supported by the library, such as ARM_V7,x86_64

Such as the Alliance’s static Common library

1. Difference between static library and dynamic library

The biggest difference:

Link mode :(dynamic link, static link)

Compilation product:

Static libraries: Collections that are compiled to produce.o files only

O + app. o => App exec (App executable)

Dynamic library: compile to produce.o => further link => Final link product (dynamic library + executable)

How to link:

Dynamic libraries and executable files at the same level, dynamic libraries -> run time -> through the path + load dynamic libraries (not allowed to be on the shelf) dynamic libraries are allowed to be on the shelf, the dynamic library is framework format, is the signature, cannot replace the path dynamic library to achieve hot update

Dynamic libraries (Dylib, Framework)

How to set up the dynamic/static libraries:

2. Podfile: use_frameworks! Dynamic # use_frameworks! staticCopy the code

2. Links between dynamic and static libraries

  • Dynamic libraries link to static libraries

    Given that cocospods imports the source code, compiles the component's source code,.xcconfig file, current buildSetting, sets editor and linker parameters, File control for buildSetting main project -> Header -> public component -> Header -> publicCopy the code
  • Link dynamic library to static library:

    O + app.o => app.exec dynamic library. O => app.exec dynamic libraryCopy the code

Problem: Symbol conflict

Void cat_test(){nslog(@"cat_test111"); Void cat_test(){nslog(@"cat_test222"); } dynamic library, compiling link in the process of conflict compilation, running normallyCopy the code

Dynamic library secondary namespace -> when the access symbol is to access the dynamic library before the access symbol. And then the notation. The secondary namespace is enabled by default

Static library: compile, run properly independent. O and app.o have the same name, why does not conflict? Ld-link Static libraries link only the code that is used, not the code that is not used. No import files will conflict

Xcfonfig configuration – ALL_load Dumplicate Symbol for Archive Static library symbol conflicts are generated, resolved: conflicting symbols are prefixed

  • Static libraries link to static libraries

B- > (A) (B)

Method 1 is successfully linked.

  1. header (api)
  2. Library path xcConfig library_search_path =
  3. Library name xcconfig OTHER_lDFLags =

App -> (static library a. o static library b. o)

  • Dynamic libraries link to dynamic libraries

App -> (DLL A (DLL B))

App runtime use, through the path. Dyld: Library not loaded… Reason: image not found

The App can connect to dynamic library A, but the App cannot connect to dynamic library B. @rpath @executable_path indicates the directory where executable programs are stored

-> A upword dependency -> A upword dependency

Apps use component code, and components use App code

How it works: To really tune up the dynamic library is to look for symbols at runtime,Copy the code
  • Dyld: Put the indirect symbols of app and dynamic library in one place,
  • Compile to binary + symbol classification (no virtual memory address allocated),
  • Merge symbol tables at link time.

So the dynamic library can also use dyld to look for symbols in the App

Xcconfig: header_search_path = (inherited)”(inherited) “(inherited)”{SRCROOT}/.. /XXAPP/” can be found now

Dynamic library A defines the symbol [[XXAppObject new] testApp]; Undifine will not compile at this point

OTHER_LDFAGS = (inherited) -xlinker -u-xlinker _OBJC_CLASS_XXAppObject

(-u marker symbol dynamic lookup symbol, causes dyld to find dynamic library and App mirror)Copy the code

4. Other

  1. Dynamic library symbols (lazy loading and non-lazy loading) do not affect compilation and are used to search

  2. The dynamic nature of oc is embodied in 1. Dynamic creation 2. Mach-o class via dyLB Runtime

  3. To compare

1. Why does the dynamic library affect the startup speed? Dyld loads the dynamic library through path (time consuming). Shared cache: 1. Static library is fast because it is combined with APP; 2. APP size: small. Shared cacheCopy the code

Symbols should be removed from App shelves, except for indirect symbols (global symbols, local symbols). Dynamic libraries (debug symbols, local symbols) can kill static libraries except for global symbols

Build Setting => Strip Style App – (All symbols) except indirect symbols (global symbols, local symbols). Remove all but Global symbols from the dynamic library’s non-global symbol and remove only debug symbols from the static library’s Debuging symbol

* Static libraries are larger than dynamic libraries when linked