美文网首页
Swift Project引用 Swift Static Lib

Swift Project引用 Swift Static Lib

作者: 轻云绿原 | 来源:发表于2019-06-05 16:39 被阅读0次

原文

So, this is the way how I create a macOS static library in Swift.

  1. Create an iOS Cocoa Touch Static Library project.

For example, name it MyStaticLib. Of cource, choose Swift for its Language.

这里不要用带数字的名字,不管是前面还是后面

  1. 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)")

  1. 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.

  1. 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)

  1. 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.

  1. Create a macOS Command Line Tool project.

Assume the name is MyCliApp and the Language for it is Swift.

  1. 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

  1. Add the library archive libMyStaticLib.a to the Linked Frameworks and Libraries of the target.
  1. 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.

相关文章

网友评论

      本文标题:Swift Project引用 Swift Static Lib

      本文链接:https://www.haomeiwen.com/subject/czdsxctx.html