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

烈酒焚心 提交于 2019-12-17 05:53:11

问题


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?


回答1:


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:

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

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

*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:

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":

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

Then all done, you can use the functions of the static libraries now.




回答2:


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

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.

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.




回答3:


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




回答4:


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

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

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`

Import module to the Swift client code[module_name]

import module_name

More examples here



来源:https://stackoverflow.com/questions/32125338/how-can-i-use-an-a-static-library-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!