How to Use an .A Static Library in Swift

How to access pure Swift Static Library from pure Swift project?

I've passed your steps and all works, at least at Xcode 11.3.1 / iOS 13.3. The only thing... it seems... see comment

import StaticLib // << looks like you've forgot this one !!!

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {


StaticLib.test("How do you do?") // error

return true
}
}

Update: Here are my steps -

  1. Click project in Xcode Project Navigator
  2. Click + to add new target
  3. Select Frameworks & Libraries > Static Library > Next
  4. Enter name StaticLib (in Swift) > Finish
  5. Open StaticLib.swift and enter
    public class StaticLib {
public class func printme() {
print("I'm swfit static lib!")
}
}

  1. Select StaticLib schema, Build > OK
  2. Select project in Project Navigator > click main application target > select General
  3. In section Framework, Libraries, and Embedded Content click + and select libStaticLib.a > Add
  4. Select main app schema > Build > OK
  5. Select AppDelegate.swift > add import StaticLib
  6. Add anywhere in code
    StaticLib.printme()

  1. Build > OK > Run ... see output

Update2: for external Lib project

+1 It is needed to copy StaticLib.swiftmodule (it is created at the same place as libStaticLib.a) into target project folder (I placed it at the level of .xcodeproj file)

+2 In main application target Build Settings set SWIFT_INCLUDE_PATHS = ${SRCROOT}

+3 Clean > Build

Note: the indicator that module is loaded is autocompletion for import - it should show StaticLib

How to use my static library (.a) in swift 3

I think you have omitted a lot of information about what you did, which makes it difficult to provide an answer with certainty. Did you do something along the lines of https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md? What is your directory structure? Where is hello.h?

Anyway, judging from the error message, one problem is that you use

   link "libhello"

in module.modulemap. It is unclear what the name of the static library is. It cannot be called helloLib.a, its name must start with lib. If it is called libhelloLib.a, then in the module map it must be

link "helloLib"

You may also want to add the -Xlinker -L/usr/local/lib option as suggested in another answer.

Hope this helps.

Static Library and Swift

As of Xcode 9 beta 4, Xcode natively supports static libraries with Swift sources.

How to use Swift static library (.a) in C++ project?

Summary: this can be made to work (probably), but if this is intended to be used in production code, then the only correct approach is to consult the Swift documentation and, if it is still the case there is no official support, ask the Swift team how to approach the problem.


You should follow whatever documentation Swift has for exporting functions in the C ABI convention. It is doubtful doing it blindly works, and if it does, it is probably by chance and may stop working at any point in the future.

Sadly, there does not seem to be official support for such thing, at least according to questions like:

  • Passing a Swift string to C
  • What is the best way to call into Swift from C?

To make any kind of FFI to work unofficially, there are several things to take into account:

  • Figure out which calling convention Swift follows, including if it uses hidden parameters or things like that. The best case scenario is that Swift uses the usual one in your system.

  • Check what are the names of the actual symbols exported by Swift. Perhaps they are mangled.

  • Research if there are any semantics to hold that the other language runtime does automatically. For instance, if some code needs to be called before/after, or perhaps something need to be initialized, etc.

On the C++ side, you should write your declaration in an extern "C" block so that the symbol is not expected to be C++-mangled:

extern "C" {
void StartWatcher();
}

There should be no need to declare it as a function pointer either.



Related Topics



Leave a reply



Submit