So, this is the way how I create a macOS static library in Swift.
- Create an iOS Cocoa Touch Static Library project.
For example, name it MyStaticLib. Of cource, choose Swift for its Language.
这里不要用带数字的名字,不管是前面还是后面
- Modify the Build Settings of the target MyStaticLib as follows:
Supported Platforms: iOS → macOS
Base SDK: Latest iOS → Latest macOS (In Xcode 9.4, it shows "Latest macOS (macOS 10.13)")
- Add any classes and methods or any types you need and build it. (Do not forget, they needs to be public or open.)
You can find libMyStaticLib.a (not red, when successfully built) in the Products group of the Project navigator of your Xcode.
- Right clicking on the product libMyStaticLib.a and Show in Finder.
The folder may be Debug or Release according to the setting of the scheme, and you find two things in it:
libMyStaticLib.a (normal archive file as found in usual static libraries)
MyStaticLib.swiftmodule folder (containing two files)
- Move them to some appropriate places.
For example, assume you have moved them to:
/usr/local/lib/libMyStaticLib.a
/usr/local/import/MyStaticLib.swiftmodule
OK, then how to use the static library in your CLI app on mac.
- Create a macOS Command Line Tool project.
Assume the name is MyCliApp and the Language for it is Swift.
- Modify the Build Settings of the target as follows:
(Assuming you have moved the products of the library as in the example above.)
Import Paths: (empty) → /usr/local/import
Library Search Paths: (empty) → /usr/local/lib
- Add the library archive libMyStaticLib.a to the Linked Frameworks and Libraries of the target.
- Write any code using the library and Run your CLI app. Do not forget to write
import MyStaticLib
.
You may need to modify some more settings, but in a simplified example I have succeeded using the static library with the settings described above. There may be (and should be) a easier way, but you can try this if you think worth trying.
网友评论