问题描述
我正在Swift中为OS X编写动态框架(Proto.framework
).我想包含来自用目标C编写的静态库(libstat.a
)中的代码.这是我得到的:
I’m writing a dynamic Framework (Proto.framework
) for OS X in Swift. I want to include code from a static library (libstat.a
) which is written in Objective C. Here’s what I’ve got:
// Dynamic.swift in Proto.framework
class Dynamic {
func doSomethingWithStat() {
Stat().statThing()
}
}
// Stat.h in libstat.a static library
@interface Stat : NSObject
- (void)statThing;
@end
// Stat.m
@implementation Stat
- (void)statThing {
NSLog(@"OK");
}
@end
在Proto.framework的目标中,我已将其链接到libstat.a.当我尝试构建Proto时,它自然不会编译,因为它找不到Stat().statThing()
的定义.它不知道我的静态库的符号.我该如何告知呢?
In my target for Proto.framework, I have linked it to libstat.a. When I try to build Proto, naturally it doesn’t compile because it can’t find the definition for Stat().statThing()
. It doesn’t know the symbols for my static library. How do I tell it about that?
对于应用程序,我将使用桥接头并执行#import <Stat/Stat.h>
.但是编译器出错并告诉我Bridging headers aren’t allowed in frameworks
.好吧.
For applications, I’d use a bridging header and do #import <Stat/Stat.h>
. But the compiler errors out and tells me Bridging headers aren’t allowed in frameworks
. OK.
因此,我将其包含在伞头"(Proto.h
)中,但这告诉了我error: include of non-modular header inside framework module
.好吧.
So I include it in my "umbrella header" (Proto.h
) but that tells me error: include of non-modular header inside framework module
. OK.
即使在全新构建后,使我的Stat
库目标Defines module: YES
似乎也无法更改错误.所以我不确定该怎么做.
Making my Stat
library target Defines module: YES
doesn’t seem to change the error even after a clean build. So I’m not sure how to do this.
有人可以指出我正确的方向吗?
Can someone point me in the right direction?
推荐答案
最简单的方法是使用模块映射文件.在下面,我假设您在单独的项目中有Proto.framework
,称为Proto
.
The easiest way to accomplish that is to use a module map file. Below I assume you have the Proto.framework
in a separate project, which is called Proto
.
- 在框架中创建一个包含以下内容的
module.modulemap
文件(根据需要替换头文件的路径):
- Create a
module.modulemap
file in your framework containing the following (substitute the path to the header file as needed):
_
framework module Proto {
umbrella header "Proto.h"
// Load a C header to be used in Swift - here /usr/include/sys/stat.h:
header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/sys/stat.h"
export *
module * { export * }
}
- 在项目构建设置中,在
Packaging
部分中找到Module Map File
.输入$(SRCROOT)/Proto/module.modulemap
- In your project build settings find
Module Map File
in sectionPackaging
. Enter$(SRCROOT)/Proto/module.modulemap
就是这样.从现在开始,您应该可以使用Swift中stat.h
中声明的任何内容.
That's it. From now on you should be able to use anything declared in stat.h
in Swift.
这篇关于如何将Objective C静态库导入Swift Framework?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!