All about libraries
After writing the post about mergeable libraries, I thought I should write a post about how libraries work in general, and capture some recommendations on how to use them.
Let’s start at the beginning, with what happens when a source file is compiled.
A compiler creates object files from source files
A compiler takes a translation unit (a source file with any imported headers), and produces a compiled object file, containing the code and data defined in the translation unit.
Exported symbols
An object file may export symbols. Exported symbols are strings that identify data structures within the object file, which may be function implementations, constants, initialized data, whatever.
// sound.c
static int tweetCount = 0;
extern void tweet(void) {
tweetCount++;
}
The file above compiles into an object file sounds.o
which exports the symbol _tweet
for the implementation of the tweet()
function1 2.
Some symbols are purely internal to the translation unit. tweetCount
, for example, is not exported.
In addition to open
/public
(exported) and private
/fileprivate
(not exported), Swift symbols have an access control option, package
, which makes the symbol visible only within a Swift package. As far as libraries are concerned, package
symbols are exported.