如何将 Objective C 静态库导入到 Swift Framework 中?

2024-04-18

我正在编写一个动态框架(Proto.framework) 对于 Swift 中的 OS X。我想包含静态库中的代码(libstat.a)这是用 Objective C 编写的。这是我得到的:

// 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()。它不知道我的静态库的符号。我该如何告诉它呢?

对于应用程序,我会使用桥接头并执行以下操作#import <Stat/Stat.h>。但编译器出错并告诉我Bridging headers aren’t allowed in frameworks. OK.

所以我将它包含在我的“伞头”中(Proto.h)但这告诉我error: include of non-modular header inside framework module. OK.

使我的Stat库目标Defines module: YES即使在干净的构建之后似乎也没有改变错误。所以我不知道该怎么做。

有人能指出我正确的方向吗?


实现这一点的最简单方法是使用模块映射文件。下面我假设你有Proto.framework在一个单独的项目中,称为Proto.

  1. 创建一个module.modulemap框架中包含以下内容的文件(根据需要替换头文件的路径):

_

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 * }
}
  1. 在您的项目构建设置中找到Module Map File在部分Packaging. Enter $(SRCROOT)/Proto/module.modulemap

就是这样。从现在开始,您应该能够使用中声明的任何内容stat.h在斯威夫特.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 Objective C 静态库导入到 Swift Framework 中? 的相关文章

随机推荐