如何在swift中使用.a静态库?

时间:2023-01-22 16:27:56

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?

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

3 个解决方案

#1


27  

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:

p.s。:2个项目在同一工作区(静态lib项目和App项目)中,静态lib项目在app项目之前构建。工作区结构如图所示:

如何在swift中使用.a静态库?

  1. In the static lib project, all the .h files are need add to "Build Phases/Copy Files":
  2. 在静态lib项目中,所有.h文件都需要添加到“Build Phases / Copy Files”:

如何在swift中使用.a静态库?

  1. Drag the static lib product file (the .a file) to the app project, see the pic:
  2. 将静态lib产品文件(.a文件)拖到app项目中,请参阅图片:

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

(路径:“app project / Build Phase / Link Binary with Libraries”)

如何在swift中使用.a静态库?

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

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

  1. Set the "Library Search Path" in you app project:
  2. 在您的app项目中设置“Library Search Path”:

如何在swift中使用.a静态库?

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

这是项目内置的.a文件路径:$(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":
  2. 为您的应用程序项目创建Bridging-Header文件,并在其中导入静态库,在我的例子中,我包含“StaticLib / StaticLib.h”和“CommonFoundation / CommonFoundation.h”:

如何在swift中使用.a静态库?

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

如何在swift中使用.a静态库?

Then all done, you can use the functions of the static libraries now. 如何在swift中使用.a静态库?

然后全部完成,您现在可以使用静态库的功能。

#2


1  

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

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

如何在swift中使用.a静态库?

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.

或者,您可以在“构建设置”下,在“搜索路径”中添加“库搜索路径”值,以包含.a文件所在文件夹的路径。

如何在swift中使用.a静态库?

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编写的,那么你需要一个Bridging Header才能在Swift中使用它。

#3


0  

With Xcode 9 using 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静态库的应用程序可能需要一组完整的构建工件,这些工件位于其原始位置

#1


27  

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:

p.s。:2个项目在同一工作区(静态lib项目和App项目)中,静态lib项目在app项目之前构建。工作区结构如图所示:

如何在swift中使用.a静态库?

  1. In the static lib project, all the .h files are need add to "Build Phases/Copy Files":
  2. 在静态lib项目中,所有.h文件都需要添加到“Build Phases / Copy Files”:

如何在swift中使用.a静态库?

  1. Drag the static lib product file (the .a file) to the app project, see the pic:
  2. 将静态lib产品文件(.a文件)拖到app项目中,请参阅图片:

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

(路径:“app project / Build Phase / Link Binary with Libraries”)

如何在swift中使用.a静态库?

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

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

  1. Set the "Library Search Path" in you app project:
  2. 在您的app项目中设置“Library Search Path”:

如何在swift中使用.a静态库?

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

这是项目内置的.a文件路径:$(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":
  2. 为您的应用程序项目创建Bridging-Header文件,并在其中导入静态库,在我的例子中,我包含“StaticLib / StaticLib.h”和“CommonFoundation / CommonFoundation.h”:

如何在swift中使用.a静态库?

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

如何在swift中使用.a静态库?

Then all done, you can use the functions of the static libraries now. 如何在swift中使用.a静态库?

然后全部完成,您现在可以使用静态库的功能。

#2


1  

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

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

如何在swift中使用.a静态库?

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.

或者,您可以在“构建设置”下,在“搜索路径”中添加“库搜索路径”值,以包含.a文件所在文件夹的路径。

如何在swift中使用.a静态库?

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编写的,那么你需要一个Bridging Header才能在Swift中使用它。

#3


0  

With Xcode 9 using 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静态库的应用程序可能需要一组完整的构建工件,这些工件位于其原始位置