如何在Swift中使用静态库和模块映射文件?

Rob*_*b N 5 module swift module-map xcode9

我正在尝试在我的Swift项目中包含一个第三方静态库。我有这两个文件,还有一些。

GoogleConversionTrackingSDK / ACTReporter.h GoogleConversionTrackingSDK / libGoogleConversionTracking.a

我将.a文件添加到目标的“链接框架和库”部分。然后module.map,我在项目中创建了一个文件,如下所示:

module GoogleConversionTracking {
    header "../../Libs/GoogleConversionTrackingSDK/ACTReporter.h"    
    export *
}
Run Code Online (Sandbox Code Playgroud)

在Swift文件中,我现在可以引用它:

import GoogleConversionTracking
Run Code Online (Sandbox Code Playgroud)

但是在链接时出现错误:

ld:-lGoogleConversionTracking找不到库
clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)
注意:-lGoogleConversionTracking找不到库

您如何解决这个问题?我不希望使用桥接头,而是尽可能使用这些模块定义文件。

Ian*_*hek 10

Module map is my synonym for trouble! Bridging headers suck, but they just work in most cases. Anyways, make sure to:

  • Configure SWIFT_INCLUDE_PATHS – a list of paths to be searched by the Swift compiler for additional Swift modules. This tells Xcode where your module maps are.
  • Configure LIBRARY_SEARCH_PATHS – this is a list of paths to folders to be searched by the linker for libraries used by the product. Xcode still needs to know where binaries for your modules are.

Also, you probably want to use the umbrella header, not just header, see documentation. I'd also suggest using modulemap extension, not sure if module.map makes difference, but that's how I remember seeing and using it in most projects.

Omar Abdelhafith 有一篇关于这个问题的邪恶博客文章,它也有助于了解其他人在处理这类事情时是如何做的。

  • 这个答案非常有帮助!将现有项目的组重命名后,SWIFT_INCLUDE_PATHS 指向一个不存在的组,这造成了很大的麻烦。 (2认同)