Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Static vs. Dynamic Linking

Static vs. Dynamic Linking

Since iOS 8, dynamic linking is also available on Apple’s mobile platform. Before the release, we had only static libraries and frameworks as options to include third-party dependencies in applications. The new opportunities offer some advantages, but have also minor drawbacks inherent. This talk should give some overview of what’s possible, help on problem discovery and points out where to pay attention.

Marius Rackwitz

February 06, 2015
Tweet

More Decks by Marius Rackwitz

Other Decks in Technology

Transcript

  1. 7

  2. A STATIC LIBRARY (AKA STATIC ARCHIVE) … is a collection

    of .o files with a table of contents that lists the global symbols in the .o files. — the man page of ld 9
  3. 10

  4. BUILDING A STATIC LIBRARY $ xcodebuild -target BananaLib … MACH_O_TYPE=staticlib

    # Pseudo (⾠) Code of what happens under the hood: PRODUCT_PATHS='' for ARCH in $ARCHS do OBJECT_FILES='' for FILE in $SRC_FILES do FILE_BASENAME=$(basename $FILE) OBJECT_FILE="$OBJECT_FILE_DIR_normal/$ARCH/$FILE_BASENAME.o" clang … -c $FILE -o $OBJECT_FILE OBJECT_FILES="$OUTPUT_FILES $OBJECT_FILE" end PRODUCT_PATH="$OBJECT_FILE_DIR_normal/$ARCH/$PRODUCT_NAME.a" ranlib $PRODUCT_PATH - $OBJECT_FILES PRODUCT_PATHS="$PRODUCT_PATHS $PRODUCT_PATH" end lipo -create $PRODUCT_PATHS -output $CONFIGURATION_BUILD_DIR/$PRODUCT_NAME.a 11
  5. STATIC LIBS ON LINK TIME ld will only pull .o

    files out of a static library if needed to resolve some symbol reference. — the man page of ld 12
  6. 13

  7. 14

  8. STATIC FRAMEWORK $ xcodebuild -target BananaFramework … MACH_O_TYPE=staticlib … #

    Build the static library mkdir $PRODUCT_NAME.framework mv $PRODUCT_NAME.a $PRODUCT_NAME.framework/$PRODUCT_NAME 16
  9. A DYNAMIC LIBRARY (AKA DYLIB OR FRAMEWORK) … is a

    final linked image. — the man page of ld 18
  10. 19

  11. DYNAMIC LIBRARY $ xcodebuild -target BananaLib … MACH_O_TYPE=mh_dylib PRODUCT_PATHS='' for

    ARCH in $ARCHS do … # Build $OBJECT_FILES PRODUCT_PATH="$OBJECT_FILE_DIR_normal/$ARCH/$PRODUCT_NAME.dylib" ld $OBJECT_FILES -o $PRODUCT_PATH -framework Foundation … PRODUCT_PATHS="$PRODUCT_PATHS $PRODUCT_PATH" end lipo -create $PRODUCT_PATHS -output $CONFIGURATION_BUILD_DIR/$PRODUCT_NAME.dylib 20
  12. DYNAMIC FRAMEWORK $ xcodebuild -target BananaFramework … MACH_O_TYPE=mh_dylib … #

    Build the dylib mkdir $PRODUCT_NAME.framework mv $PRODUCT_NAME.dylib $PRODUCT_NAME.framework/$PRODUCT_NAME 21
  13. DYLIBS ON LINK TIME Exported symbols from the dynamic library

    are used to resolve references. — the man page of ld 22
  14. 23

  15. 24

  16. 25

  17. 26

  18. 27

  19. 28

  20. 29

  21. 30

  22. DYLIBS ON LINK TIME The generated final linked image will

    have encoded that it depends on that dynamic library. — the man page of ld 31
  23. $ otool -l BananaKit.framework/BananaKit BananaKit.framework/BananaKit: … Load command 3 cmd

    LC_ID_DYLIB cmdsize 96 name @rpath/BananaKit.framework/BananaKit (offset 24) time stamp 1 Thu Jan 1 01:00:01 1970 current version 1.0.0 compatibility version 1.0.0 … Load command 9 cmd LC_LOAD_DYLIB cmdsize 56 name @rpath/libswiftCore.dylib (offset 24) time stamp 2 Wed Dec 31 16:00:02 1969 current version 0.0.0 compatibility version 0.0.0 … Load command 22 cmd LC_RPATH cmdsize 40 path @executable_path/Frameworks (offset 12) Load command 23 cmd LC_RPATH cmdsize 40 path @loader_path/Frameworks (offset 12) … 33
  24. @rpath? ▸ Should be used as prefix in LC_ID_DYLIB /

    LC_LOAD_DYLIB ▸ Searched by dyld at runtime. 35
  25. ADVANTAGES OF DYNAMIC FRAMEWORKS ▸ Easier to distribute & integrate

    if compiled ▸ Encoded Dependencies ▸ Two-Level Namespacing ▸ Reduces file size if used for app & extensions ▸ Separate resources in distinct bundles 37
  26. ADVANTAGES OF DYNAMIC FRAMEWORKS ▸ Easier to distribute & integrate

    if compiled ▸ Encoded Dependencies ▸ Two-Level Namespacing ▸ Reduces file size if used for app & extensions ▸ Separate resources in distinct bundles 38