如何将静态库(C++)包裹在XCFramework中以供Swift使用?
为我提供了一个静态库libexample.a
以及一堆C ++标头,我需要在iOS应用中使用。二进制是脂肪,其中包含用于iPhoneOS-ARM64/e和iPhonesimulator-x64的对象。
我已经对该主题进行了一些研究,得出的结论是,使用XCFramework将是最好的事情。尽管如此,我仍然感到完全超出了自己的深度,因为这是我第一次尝试任何类型的事情。
到目前为止,我已经完成了
1个。从库文件中创建xcframework
lipo -tracttract
从脂肪二进制xcodebuild -create -create -xcframework -library libarry lib -arm64.a-headers -headers -headers -headers -headers -library .. library .. 。
我在示例中看到的文件夹还缺少很多文件。这似乎是错误的方法,所以我尝试了...
2。创建一个框架,然后从IT
文件
- 中创建一个XCFramework创建新的Objective-C Framework Project
exampleframework
- 拉我所有的标头,将fat二进制添加到项目中
- ,将我的所有标题添加到
exampleframework.h
雨伞标题 - 创建以下
exampleframework.modulemap
:
framework module ExampleFramework {
umbrella header "ExampleFramework.h”
link "Example"
export *
module * { export * }
}
settings
- 常规:
1.1。将libexample.a
添加到框架&库 - 构建阶段:
2.1。使所有标题公开
2.2。将libexample.a
添加到链接二进制
2.3。将libexample.a
添加到复制Bundle Resources - 构建设置:
3.1。 跳过安装:否
3.2。 构建发行库:是
3.3。 模块映射文件:exampleframework/exampleframework.modulemap
3.4。 定义模块:是
3.5。 编译源为:objection-c ++
i然后将框架存档,用于iPhoneos
和iphonesimulator
分别:
xcodebuild archive \
-scheme "ExampleFramework" ONLY_ACTIVE_ARCH=NO \
-archivePath "path/to/ExampleFramework_${SDK}.xcarchive" \
-sdk ${SDK} \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
...并从输出中生成了XCFramework:
xcodebuild -create-xcframework \
-framework "path/to/ExampleFramework_iphoneos.xcarchive/Products/Library/Frameworks/ExampleFramework.framework"
-framework ...
-output "path/to/ExampleFramework.xcframework"
构建错误
我从此看起来很有希望的文件夹结构,因此我尝试将XCFramework添加到我的iOS项目中。现在发现了该模块,但是Xcode停止在第一个#include上编译,称其无法找到stdexcept
,我从中得出结论,它缺少C ++标准库标头。
我尝试在构建设置中设置标题搜索路径。寻找与我相关的一对夫妇的正确路径:
/Applications/Xcode_13.2.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
/Applications/Xcode_13.2.1.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include
/Applications/Xcode_13.2.1.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include
取决于我选择哪一个以及是否在构建前清洁,我会遇到一堆不同的错误(似乎是随机的)。主要:
未知类型名称'namespace'
以及许多其他类型,这会导致我相信Xcode并不识别标头为C ++。
或:
对模块'darwin'的循环依赖性:darwin-> std - >达尔文
我真的不知道,因此任何解决方案或建议将我指向正确的方向,我 都会赞赏。我也可以接受一些完全不同的方法,我只想以某种方式使用图书馆。
谢谢!
I have been given a static library libExample.a
together with a bunch of C++ headers, which I need to use in an iOS app. The binary is fat, containing objects for iphoneos-arm64/e and iphonesimulator-x64.
I have done some research on the subject and came to the conclusion, that using a XCFramework would be the best thing to do. Still, I feel completely out of my depth, since this is my first time trying anything of the sort.
What I have done so far
1. Creating the XCFramework from library files
lipo -extract
architectures from fat binaryxcodebuild -create-xcframework -library LIB-arm64.a -headers HEADERS -library ...
Importing the XCFramework into my Swift project didn't yield any usable modules. Also the folder was missing a lot of the files, I've seen in examples. It seemed like the wrong way to go about it, so I tried...
2. Creating a Framework and then a XCFramework from it
Files
- Create new Objective-C Framework project
ExampleFramework
- Pull all my headers and the fat binary into the project
- Add all my headers to the
ExampleFramework.h
umbrella header - Create the following
ExampleFramework.modulemap
:
framework module ExampleFramework {
umbrella header "ExampleFramework.h”
link "Example"
export *
module * { export * }
}
Settings
- General:
1.1. AddlibExample.a
to Frameworks & Libraries - Build Phases:
2.1. Make all headers public
2.2. AddlibExample.a
to Link Binary with Libraries
2.3. AddlibExample.a
to Copy Bundle Resources - Build Settings:
3.1. Skip Install :No
3.2. Build Libraries for distribution :Yes
3.3. Module map file:ExampleFramework/ExampleFramework.modulemap
3.4. Defines modules:Yes
3.5. Compile Sources As:Objective-C++
I then archived the framework, for iphoneos
and iphonesimulator
respectively:
xcodebuild archive \
-scheme "ExampleFramework" ONLY_ACTIVE_ARCH=NO \
-archivePath "path/to/ExampleFramework_${SDK}.xcarchive" \
-sdk ${SDK} \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
...and generated a XCFramework from the outputs:
xcodebuild -create-xcframework \
-framework "path/to/ExampleFramework_iphoneos.xcarchive/Products/Library/Frameworks/ExampleFramework.framework"
-framework ...
-output "path/to/ExampleFramework.xcframework"
Build Errors
The folder structure I got from this looked promising, so I tried adding the XCFramework to my iOS project. The module was being found now, but XCode stopped compiling at the first #include, saying it wasn't able to find stdexcept
, from which I concluded it was missing the C++ standard library headers.
I tried setting Header Search Paths in Build Settings. Looking for the right path I found a couple, that looked relevant to me:
/Applications/Xcode_13.2.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
/Applications/Xcode_13.2.1.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include
/Applications/Xcode_13.2.1.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include
Depending on which one I choose and whether I clean before building, I'm getting a bunch of different errors (it seems random). Mainly:
Unknown type name 'namespace'
and lots and lots of others, that lead to me believe XCode doesn't recognize the headers as C++.
Or:
Cyclic dependency on module 'Darwin' : Darwin -> std -> Darwin
I really have no idea at this point, so any solutions or suggestions pointing me into the right direction, would me much appreciated. I'd also be open to some completely different approach, I just want to use the library somehow.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用于来自模块的参考。modulemap
For reference from module.modulemap