Swift. Static and Dynamic Libraries. Frameworks
Here is a quick introduction, along with the full articles list
There are a lot of articles that tell about libraries and frameworks in Swift. At the bottom of this page, you will find links to materials I highly recommend to get acquainted with
The goal of my work isn’t to produce one more article that tells the same as dozens before. Even more, most of them are really great!
In this article, I tried to visualize the process of compiling, linking, and simply working with libraries and frameworks. I hope this work will be a good extension of materials you can find all over the Internet and in the Apple documentation
Well, let’s get started with the types of code:
Static libraries are fully embedded into the result executable binary
Dynamic ones behave differently. The result executable binary has just references to the functions of that libraries. And the code of the functions is loaded at runtime
Let’s take a look at what Xcode and iPhone do under the hood:
And now let’s see what frameworks are:
To summarize everything above, let’s say that in iOS/macOS development, there are two types of modules:
- library, and its synonyms like a static library, static framework
- framework, and its synonyms like a dynamic framework
Telling about libraries or static frameworks, people assume static libraries — modules, that are fully embedded into the result executable binary
And speaking about frameworks, they refer to dynamic libraries that are wrapped by frameworks and linked to the executable binary via references to their functions
There are several helpful terminal commands that allow you to determine the type of binary and get some useful information:
In our project, we use cocoapods as Swift dependency manager. As a bonus, let’s see how Xcode treats its dependencies and how they are transformed into static libraries or dynamic frameworks:
I hope it will help you to get closer to the Swift and iOS development and to have a better understanding of not so simple topic :)
Here is the second part of this work, where we will discuss bundles and packages. It should give you a more comprehensive picture of what apps are
Resources
- https://www.vadimbulavin.com/static-dynamic-frameworks-and-libraries/
- https://www.vadimbulavin.com/xcode-build-system/
- https://medium.com/@zippicoder/libraries-frameworks-swift-packages-whats-the-difference-764f371444cd
- https://medium.com/joshtastic-blog/frameworks-and-libraries-in-swift-2359e4274faa
- https://gist.github.com/SheldonWangRJT/78c9bd3b98488487c59a6a4a9c35162c
- https://stackoverflow.com/questions/13082149/in-xcode-project-target-build-settings-what-is-mach-o-type
- https://medium.com/tokopedia-engineering/a-curious-case-of-mach-o-executable-26d5ecadd995
- https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/OverviewOfDynamicLibraries.html#//apple_ref/doc/uid/TP40001873-SW1
- https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WhatAreFrameworks.html#//apple_ref/doc/uid/20002303-BBCEIJFI
- https://www.objc.io/issues/6-build-tools/mach-o-executables/