如何在一个框架,使用C dylib从斯威夫特文件 [英] How to use a C dylib from a Swift file in a framework
问题描述
我在看起一个框架,它使用 libz.dylib
使用斯威夫特,但是看起来没有办法从内部斯威夫特导入。我试图进口zlib的
和进口libz进行
,它没有工作。的ZLib已链接到目标。
好像让我的雨燕code看到的zlib
类的唯一方法是导入必要的标头中的一个桥接报头,但框架目标可以'T有一个桥接报头,所以有使用dylib的方法吗?
在这个答案说明你可以导入系统模块:<一href=\"http://stackoverflow.com/questions/25248598/importing-commoncrypto-in-a-swift-framework\">Importing CommonCrypto在斯威夫特框架
TLDR
- 添加一个文件夹命名为
的ZLib
来你的框架目录 - 添加
module.map
文件包含以下内容
模块的ZLib [系统] {
头\"/Applications/X$c$c-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/zlib.h\"
出口 *
}
您可能需要在路径更改为您的ZLIB头。该示例适用于X code 7 Beta版和埃尔卡皮坦。
<醇开始=3>I'm looking at starting to use Swift in a framework, which uses libz.dylib
, but it looks like there's no way to import it from within Swift. I tried import zlib
and import libz
, which didn't work. ZLib is already linked to the target.
It seems like the only way to get my Swift code to see the zlib
classes is to import the necessary headers in a bridging header, but framework targets can't have a bridging header, so is there a way to use a dylib?
You can import system modules as described in this answer: Importing CommonCrypto in a Swift framework
TLDR
- Add a Folder named
ZLib
to your framework directory - Add a
module.map
file with the following contents
module ZLib [system] {
header "/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/zlib.h"
export *
}
You might have to change the path to your zlib header. The example works for XCode 7 Beta and El Capitan.
- Add the currently generated module.map file to your build settings
- Import ZLib in your swift file and you will be ready to go
这篇关于如何在一个框架,使用C dylib从斯威夫特文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!