ios 如何快速使用 .a 静态库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32125338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:20:59  来源:igfitidea点击:

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

iosswiftstatic-librarieswebrtc

提问by CarlosAndres

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?

我想快速使用我的 webrtc .a 静态库。你能帮忙吗?我读到你不能在 swift 中使用静态库,是真的吗?

回答by Raniys

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:

ps:2个项目在同一个工作区(静态库项目和App项目),静态库项目是在app项目之前构建的。工作区结构如图所示:

/Users/raniys/Desktop/Screen Shot 2016-03-14 at 5.27.20 PM.png

/Users/ranys/Desktop/Scr​​een 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":
  1. 在静态 lib 项目中,所有 .h 文件都需要添加到“构建阶段/复制文件”中:

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

  1. 将静态库产品文件(.a 文件)拖到应用程序项目中,见图:

(路径:“应用程序项目/构建阶段/将二进制文件与库链接”)

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:

*如果你关心.a文件的红色标记,你只需要选择“通用iOS设备”作为构建设备重新构建静态lib项目,并将.a重新拖到app项目中(红色的可以删除)

  1. 在您的应用项目中设置“图书馆搜索路径”:

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

这是项目内置的 .a 文件路径:$(USER_LIBRARY_DIR)/Developer/Xcode/DerivedData/StockApp-fkjqyvsniiauxogkivalcduqeotj/Build/Products/Debug-iphoneos

  1. 为您的应用项目创建 Bridging-Header 文件,并在其中导入静态库,在我的情况下,我包括“StaticLib/StaticLib.h”和“CommonFoundation/CommonFoundation.h”:

enter image description here

在此处输入图片说明

  1. Add the Bridging-Header file path to "Objective-C Bridging Header" of you app project:
  1. 将 Bridging-Header 文件路径添加到您的应用项目的“Objective-C Bridging Header”:

enter image description here

在此处输入图片说明

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

大功告成,现在就可以使用静态库的功能了。 在此处输入图片说明

回答by yoAlex5

Using Objective-C Classes in Swift

在 Swift 中使用 Objective-C 类

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

如果您要在 App Target 中导入代码(在一个项目中混合 Swift 和 Objective-C),您应该使用bridging headerfile 将 Objective-C 代码公开给 Swift 代码。[在一个项目中混合使用 Swift 和 Objective-C 代码]

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

在这篇文章中,我将描述如何将 Objective-C 静态库导入 Swift 代码

Swift consumer -> Objective-C static library

Swift 消费者 -> Objective-C 静态库

Xcode version 10.2.1

Xcode 版本 10.2.1

Create Objective-C static library

创建 Objective-C 静态库

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.modulemapfile [About]

创建module.modulemap文件[关于]

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

Create module_name-umbrella.hfile[About]and add all .hfiles that will be open for consumer

创建module_name-umbrella.h文件[About]并添加所有.h将对消费者开放的文件

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

Add module.modulemapand module_name-umbrella.hfiles into the Copy Filessection[About].

module.modulemapmodule_name-umbrella.h文件添加到[About]Copy Files部分。

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

Add Implementation files .m

添加实现文件 .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 .hthat were listed in module_name-umbrella.h(header_1.h, header_2.h)[can not do it][public target membership]

添加( , ).h中列出的头文件[不能这样做] [公共目标成员资格]module_name-umbrella.hheader_1.hheader_2.h

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+ Bor Product -> Build

构建库 - ? Command+BProduct -> 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 Filessection
  • usr/local/includefolder with files from Headerssection. There are public interfaces/definitions
  • lib<product_name>.a– 一个内置的静态库
  • include/<product_name>包含Copy Files部分文件的文件夹
  • usr/local/include包含Headers部分文件的文件夹。有公共接口/定义

Swift consumer with Objective-C static library

带有 Objective-C 静态库的 Swift 使用者

Drag and dropthe binary into the Xcode project[About]

Drag and drop将二进制文件导入 Xcode 项目[关于]

Link Library[Undefined symbols][Link vs Embed]

Link Library[未定义的符号] [链接与嵌入]

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]

添加Library Search paths[未找到库] [递归路径]

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]

添加Header Search Paths【找不到模块】【递归路径】

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]

将模块导入 Swift 客户端代码[module_name]

import module_name

More examples here

更多例子在这里

回答by Cole

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

是的,您可以在 Swift 中使用静态库。转到您的构建阶段并在“将二进制文件与库链接”下并将它们添加到那里。

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.

或者,您可以进入 Build Settings 并在“Search Paths”中附加“Library Search Paths”值以包含 .a 文件所在文件夹的路径。

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.

还要记住,如果这个库是用 Objective-C 编写的,你将需要一个桥接头才能在 Swift 中使用它。

回答by abdullahselek

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

使用Xcode 9使用Swift支持的静态库。您可以在 Objective-C 中使用 like 。Xcode 发行说明

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

Xcode 支持包含 Swift 代码的静态库目标。调试使用 Swift 静态库的应用程序可能需要一整套位于其原始位置的构建工件