Slide 1

Slide 1 text

All about linking libraries How to solve import/link errors

Slide 2

Slide 2 text

How do you use libraries or frameworks in Swift project?

Slide 3

Slide 3 text

Dragging to Embedded Binaries

Slide 4

Slide 4 text

Import frameworks without modules (Some ObjC vendored frameworks)

Slide 5

Slide 5 text

Dynamic vs Static w/ Bundle vs wo/Bundle Static Library (*.a) Static Framework (*.framework) Dynamic Framework (*.framework) Dynamic Library (*.dylib)

Slide 6

Slide 6 text

with Bundle without Bundle Static Linking Dynamic Linking Dynamic vs Static w/ Bundle vs wo/Bundle Static Library Static Framework Dynamic Library Dynamic Framework

Slide 7

Slide 7 text

Anatomy of linking libraries or frameworks

Slide 8

Slide 8 text

How to make available external frameworks/libraries in Swift • Import the framework/library from your app or framework • Link the framework/library to your app or framework

Slide 9

Slide 9 text

How Swift import frameworks/libraries

Slide 10

Slide 10 text

What need to import libraries into Swift?

Slide 11

Slide 11 text

Module

Slide 12

Slide 12 text

Frameworks can contain modules

Slide 13

Slide 13 text

Some frameworks don't have modules

Slide 14

Slide 14 text

Any Library doesn't have modules Static Library (*.a) Dynamic Library (*.dylib)

Slide 15

Slide 15 text

Anatomy: importing libraries or frameworks

Slide 16

Slide 16 text

How to import frameworks • Does the framework has modules? Yes: => Set parent directory of the framework to Framework Search Paths

Slide 17

Slide 17 text

Set parent directory of the framework to Framework Search Paths

Slide 18

Slide 18 text

How to import frameworks • Does the framework has modules? No: • Do you try to import the framework to your app? Yes: => a) Create module map. b) Create bridging header. No: => Create module map.

Slide 19

Slide 19 text

a) Bridging Header

Slide 20

Slide 20 text

a) Bridging Header

Slide 21

Slide 21 text

a.) Bridging Header

Slide 22

Slide 22 text

b) Module Map

Slide 23

Slide 23 text

b) Module Map

Slide 24

Slide 24 text

b) Module Map module Marketo { umbrella header "../../Frameworks/Marketo.framework/Headers/Marketo.h" export * module * { export * } }

Slide 25

Slide 25 text

b.) Module Map

Slide 26

Slide 26 text

Import libraries

Slide 27

Slide 27 text

How to import libraries • a) Create bridging header. • b) Create module map.

Slide 28

Slide 28 text

Import libraries

Slide 29

Slide 29 text

a) Bridging Header

Slide 30

Slide 30 text

b) Module Map module WebPDecoder { header "include/decode.h" header "include/types.h" }

Slide 31

Slide 31 text

Anatomy: linking libraries or frameworks

Slide 32

Slide 32 text

How to link frameworks • Is the framework dynamic framework? Yes: => Copy the framework into the app bundle => Set parent directory of the framework to Framework Search Paths => Set -framework to Other Linker Flags No: => Set parent directory of the framework to Framework Search Paths => Set -framework to Other Linker Flags e.g.) -framework KeychainAccess

Slide 33

Slide 33 text

Link dynamic frameworks

Slide 34

Slide 34 text

Copy frameworks into app bundles

Slide 35

Slide 35 text

Link frameworks

Slide 36

Slide 36 text

How to link libraries • Is the library dynamic library? Yes: => Set parent directory of the framework to Library Search Paths => Copy the library into the app bundle No: => Set parent directory of the framework to Library Search Paths => Set -l to Other Linker Flags e.g.) -lwebpdecoder

Slide 37

Slide 37 text

Link libraries

Slide 38

Slide 38 text

You might see... => Correct module map => Make sure Header Search Paths is correct

Slide 39

Slide 39 text

Recap • The processes of making external libraries available in the Swift project. => Import and Linking • Errors on importing => Module maps, Framework/Library Search Paths, Import Paths • Errors on linking => Module maps, Framework/Library Search Paths, Static or Dynamic • Errors on runtime => "Copy Files" Build phase

Slide 40

Slide 40 text

Resources • http://nsomar.com/modular-framework-creating-and-using-them/ • https://theswiftdev.com/2018/01/25/deep-dive-into-swift-frameworks/ • https://samsymons.com/blog/understanding-objective-c-modules/ • http://clang.llvm.org/docs/Modules.html