How can I use an .a static library in swift?
Asked Answered
S

4

40

I want to use my webrtc .a static library in swift. Can you help please? I read you can´t use static libraries in swift, is that true?

Sassan answered 20/8, 2015 at 18:6 Comment(4)
You can use static libraries in Swift. You can't create static libraries from Swift.Galvin
that is not correct. You can create static libraries. I even link swift code with some static libraries in order to create a single .a file.Pollinosis
Just create a module.modulemap file with umbrella header. Make sure that it is can be found by Swift compiler (e.g. add module.modulemap to "Copy Files" build phase alongside with ObjC headers).Roeder
More details plz @RoederKiersten
N
46

Have you fixed this problem you asked? I meet this problem on today too, and I've fixed it just a moment. If you don't have fixed this problem, you can try the below steps:

p.s.: the 2 projects are in the same workspace (the static lib project and the App project), the static lib project is build before the app project. The workspace structure as the pic shows:

https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfxcF_jZRfRX7ImaFf1KmMva3/Users/raniys/Desktop/Screen Shot 2016-03-14 at 5.27.20 PM.png

  1. In the static lib project, all the .h files are need add to "Build Phases/Copy Files":

enter image description here

  1. Drag the static lib product file (the .a file) to the app project, see the pic:

(path: "app project/Build Phases/Link Binary With Libraries")

enter image description here

*if you care about the red color mark for the .a file, you just need choose the "generic iOS device" as the build device to re-build the static lib project, and re-drag the .a to the app project(the red one can be delete)

  1. Set the "Library Search Path" in you app project:

enter image description here

this is the .a file path which the project built in: $(USER_LIBRARY_DIR)/Developer/Xcode/DerivedData/StockApp-fkjqyvsniiauxogkivalcduqeotj/Build/Products/Debug-iphoneos

  1. Create Bridging-Header file for your app project, and import the static lib in it, in my case I include "StaticLib/StaticLib.h" and "CommonFoundation/CommonFoundation.h":

enter image description here

  1. Add the Bridging-Header file path to "Objective-C Bridging Header" of you app project:

enter image description here

Then all done, you can use the functions of the static libraries now. enter image description here

Newfoundland answered 14/3, 2016 at 9:57 Comment(0)
A
9

*Using Objective-C Classes in Swift

If you are going to import code within an App Target (Mixing Swift and Objective-C in one project) you should use bridging header file to expose Objective-C code to Swift code.

[Mixing Swift and Objective-C code in a project]

In this post I will describe how to import Objective-C static library to Swift code

Swift consumer -> Objective-C static library

Xcode version 10.2.1

1. Create Objective-C static library

Create a library project or create a library target

File -> New -> Project... -> Cocoa Touch Static Library
//or
Project editor -> Add a Target -> Cocoa Touch Static Library 

Create module.modulemap file [About]

module module_name {
    umbrella header "module_name-umbrella.h"
    export *
}

Create module_name-umbrella.h file[About] and add all .h files that will be open for consumer

#import "header_1.h"
#import "header_2.h"

Add module.modulemap and module_name-umbrella.h files into the Copy Files section[About].

Project editor -> select a target -> Build Phases -> Copy Files -> add `module.modulemap`, `module_name-umbrella.h` 

Add Implementation files .m

Select `.m` file -> Select File Inspectors Tab -> Target Membership -> Select the target
//or
Project editor -> select a target -> Build Phases -> Compile Sources -> add files

Add Headers files .h that were listed in module_name-umbrella.h(header_1.h, header_2.h)[can not do it] [public target membership]

Select `.h` file -> Select File Inspectors Tab -> Target Membership -> Select the target and make it **public**
//or
Project editor -> select a target -> Build Phases -> Headers -> add files to the **public** zone

Build library - ⌘ Command + B or Product -> Build

Note: Be sure that you build library for the same process architecture as the client code.

Find generated output[Build location]

Products group -> lib<product_name>.a -> Show in Finder

The directory includes

  • lib<product_name>.a – a built static library
  • include/<product_name> folder with files from Copy Files section
  • usr/local/include folder with files from Headers section. There are public interfaces/definitions

2. Swift consumer with Objective-C static library

Drag and drop the binary into the Xcode project[About]

Link Library[Undefined symbols] [Link vs Embed]

Project editor -> select a target -> General -> Linked Frameworks and Libraries -> add -> Add Others... -> point to `lib<product_name>.a` file
//or
Project editor -> select a target -> Build Phases -> Link Binary With Libraries -> add -> Add Others... -> point to `lib<product_name>.a` file

Add Library Search paths[Library not found for] [Recursive path]

Project editor -> select a target -> Build Settings -> Search Paths -> Library Search paths -> add path to the parent of `lib<product_name>.a` file

Add Header Search Paths[Module not found] [Recursive path]

Project editor -> select a target -> Build Settings -> Search Paths -> Header Search Paths -> add path to generated `include/<product_name>` folder with `module.modulemap` and `<product_name>-umbrella.h`

3. Consume Objective-C static library from Swift

Import module to the Swift client code[module_name]

import module_name

[More examples here]

Amputate answered 6/12, 2019 at 15:28 Comment(2)
Thanks a lot for the detailed explanation @AmputateLathi
Hey, how can we handle if my project to be converted in the library is not pure objective C, actually it is a mixup of Objective C and Swift. So the library is not getting compiled as it's showing me an error on every file where I have imported "ProjectName-Sift.h". On that line getting an error as "Project-Swift.h file not found"Dietsche
N
2

Yes, you can use static libraries in Swift. Go to your Build Phases and under "Link Binary With Libraries" and add them there.

enter image description here

Alternatively, you can go under Build Settings and in "Search Paths" append the "Library Search Paths" value to include the path to the folder that your .a file is in.

enter image description here

You can add headers for your library the same way under the "Header Search Paths"

Also keep in mind that if this library is written in Objective-C, you will need a Bridging Header to use it in Swift.

Nightcap answered 20/8, 2015 at 19:27 Comment(1)
@jchnxu is it possible that you're getting that error because Xcode cannot find your headers to the module? Perhaps adding the Recursive option if your path is in header search paths will allow Xcode to recurse into the .a moduleNightcap
P
1

With Xcode 9 using static libraries supported with Swift. You can use like using in Objective-C. Xcode Release Notes

Xcode supports static library targets which contain Swift code. Debugging applications that use Swift static libraries may require a complete set of build artifacts that are in their original location

Paganini answered 8/5, 2018 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.