Frameworks and Libraries
This write-up is an attempt to organize information about using Frameworks and Libraries for macOS, all in one place.
It's a challenge to come up with a scheme for organizing things. Some questions that arise:
- in what language is the helper function, library or framework written?
- is it from macOS or from me, the user?
- where is the library or framework currently located?
- in what language is the caller written?
- are we compiling on the command line, calling from a Cocoa app or what?
Roughly speaking, we'll try to organize things that way, from the outside in.
An outline of what we'll look at is
- classic C libraries, both static and dynamic
- C libraries wrapped into Cocoa frameworks
- calling libraries from Objective-C
- calling libraries from Swift
- writing and using Swift frameworks
One problem I've noticed in doing such tests, is that one can easily be confused into thinking something you tried works, but it turns out to have actually used object code or even files left over from a previous attempt.
To avoid these problems, especially with C code, we can follow the steps here.
So here is a list of the short focused items we'll cover.
C code libraries
a C static lib on the Desktop from the command line
a C static library in ~/Library/Frameworks from the command line
a C static library in ~/Library/Frameworks from a Cocoa Objective-C app
Objective-C Framework wrapping our C files
a C dynamic library
dynamic library in [/usr/local/lib] or
~/Library/Frameworks, from the command line
For the difference between static and dynamic libraries see here.
The main difference is that a dynamic library can be shared between different programs, and its code isn't actually present in the object code.
One value in looking at these older ways of doing things is just to have some insight into what a library is.
Also, a number of libraries are available for macOS through various package managers including Homebrew.
[not written yet]
The modern way on macOS is to use a Framework.
- OS X Framework from the command line
Certainly the simplest way to use C code in a modern Cocoa app is to just add the .c files to the Xcode project. If you already have something like
libadd.dylib then things are (much) more complicated.
I found an article that describes how to repackage legacy libraries into Frameworks. I haven't worked through all that yet.
Swift calling Objective-C frameworks
Getting a (seedable) rand function back into Swift.
Using the RandFW framework to make a command line demo for encryption and decryption.
Objective-C framework including our C files called by Swift on the command line.
Objective-C framework including our C files called from a Swift Cocoa app.