【问题标题】:Private module map for a framework框架的私有模块映射
【发布时间】:2016-03-19 20:00:37
【问题描述】:

我正在使用 this answer 创建一个模块映射来为 CommonCrypto 创建一个模块,以便我可以在框架中使用它。

但是,这样做意味着我使用此框架的任何项目都可以使用 import CommonCrypto 访问 CommonCrypto - 更糟糕的是,在另一个框架中声明 CommonCrypto 并将其导入项目会导致 Redefinition of module 'CommonCrypto' 错误。

即以下设置:

MainProject
    |--> import FrameworkA - module map for CommonCrypto
    |--> import FrameworkB - module map for CommonCrypto

有没有一种方法可以创建一个模块映射,但让它私有于它创建/使用的框架? (很像框架的 Swift 中的 internal 访问属性)。 llvm Clang docs 显示 private attribute 但我无法确定将其放在模块映射中的哪个位置,甚至可能不是为了这个目的! 还有一个export attribute,但我也不完全确定如何使用它...!

这是我用于 CommonCrypto 的模块映射 - $(SDKROOT) 在构建阶段被换出到正确的位置(对于 iphoneosiphonesimulator SDK):

module CommonCrypto [system] [extern_c] {
    umbrella header "$(SDKROOT)/usr/include/CommonCrypto/CommonCrypto.h"
    export *
}

FrameworkA / FrameworkB 中使用这很好(除了你不能“去定义”,但我不介意)。

【问题讨论】:

  • 嗨,这有什么更新吗?我对 libz 有相同的设置 :(
  • Rich,能否请您提供替换 $(SKROOT) 的构建阶段脚本?谢谢!
  • @appleitung:脚本在这里:gist.github.com/rhodgkins/5eecee8bcbdb6021fc798247132e9fa7 然后像这样设置它:postimg.org/image/fj7j9nsqp 在一个项目中。不要忘记将目录 $(PROJECT_DIR)/$(TARGET_NAME)/ExternalFrameworks/ 添加到 *Framework Search Paths 构建设置中。然后所有模块映射应位于输入目录文件夹中 - 包含模块映射的文件夹名称为名称。

标签: xcode swift frameworks dylib llvm-clang


【解决方案1】:

免责声明:我没有为CommonCrypto 尝试过这个,但它适用于libz 的情况

一个可能的解决方案是创建一个module.private.modulemap,如Clang documentation中所述

因此,例如在 FrameworkA 中,您可以为 FrameworkA 编写一个 module.modulemap 文件,如下所示:

module FrameworkACommon {
}

然后你会像这样创建一个module.private.modulemap 文件

explicit  FrameworkACommon.Crypto [system] [extern_c] {
   header "/Applications/Xcode6-Beta5.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/usr/include/CommonCrypto/CommonCrypto.h"
    link "CommonCrypto"
    export *
}

然后对 FrameworkB 重复。

现在 CommonCrypto 是 FrameworkA 和 FrameworkB 中的私有模块,名称不会冲突。

【讨论】:

  • 不错!感谢分享 - 我会试试看!
  • @tmpz 我得到了很多'使用未声明的类型......'。你遇到过这种情况吗?
  • 您可能会得到未声明的类型,因为您的模块没有导出任何标头。 export * 会将标头导出到 FrameworkACommon 并导入它,您必须编写 FrameworkACommon.Crypto 但它会停在那里。导出的标头在 FrameworkACommon 之后将不可见,它们现在是内部的。
  • 有没有办法让这种事情使用 CocoaPods 工作?我已经用模块图尝试了很多东西,但我似乎无法让它工作
  • @cjwirth 您可以为 CocoaPod 规范指定 module map
猜你喜欢
  • 2016-05-27
  • 2011-11-29
  • 2020-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
相关资源
最近更新 更多