【问题标题】:Importing a C library into a Swift project将 C 库导入 Swift 项目
【发布时间】:2016-10-17 07:04:11
【问题描述】:

我正在尝试将 libsass(一个 C 库)导入到我用 Swift 编写的 OSX 项目中。但不知何故,我正在处理很多依赖问题。顺便说一句,我在这里使用本指南中的 Module maps 方法:https://spin.atomicobject.com/2015/02/23/c-libraries-swift/。


libsass 存储库:https://github.com/sass/libsass

我可以在 c: 中运行 version.c(提供的示例 libsass)程序:

#include <stdio.h>
#include "sass/context.h"
int main() {
  puts(libsass_version());
  return 0;
}

我的方法是在“libsass/include”目录中导入头文件。

这是迄今为止的尝试:

1我建了一个module.map文件,放在libsass目录下。

module libsass [system] {
    header "include/sass.h"
    export *
}

2然后我将目录添加到我的 swift 项目中

3 设置 Swift Compiler > Search Paths > Import Paths to '${SRCROOT}/libsass'

4 构建,我遇到了这个:

'sass/base.h' file not found with <angled> include; use "quotes" instead
'sass/version.h' file not found with <angled> include; use "quotes" instead
'sass/values.h' file not found with <angled> include; use "quotes" instead

5 构建设置 > 搜索路径 > 标头搜索路径设置为“$(PROJECT_DIR)/libsass/include”

构建设置 > 搜索路径 > 库搜索路径设置为“$(PROJECT_DIR)/libsass/include”

这次我没有看到之前的错误但是在test_superselector.cpp中遇到了一个新的错误:

'../ast.hpp' file not found 

6 我在 Build settings > Linking > Other Linker Flags 中添加了“-lsass”。由于在 libsass 提供的示例中,他们使用 -lsass 标志来编译 c 代码。

同样,不好。

我花了两天时间试图弄清楚这一点,但没有明显的进展。请查看 libsass 存储库。任何帮助表示赞赏。 谢谢!!

【问题讨论】:

    标签: ios c xcode swift


    【解决方案1】:

    我简要地查看了 github 上的 libsass 代码。找不到ast.hpp 文件,因为您必须已将 libsass 源目录添加到 Xcode 项目中,并且它正在尝试将库源构建为项目的一部分。

    由于您在链接器标志中添加了-lsass,我认为您正在尝试在项目中使用已经构建的库,这是一种更简洁的方法,这就是模块映射有用的地方。

    您不应该将 libsass 目录添加到您的 Xcode 项目中。您可以将包含 libsass 头文件和库的目录放在您的项目目录中,这与将它们添加到您的项目中是不同的。

    module.map 中的header 行应具有标题的绝对路径或相对于module.map 位置的路径。

    其他一些观察:

    • 库搜索路径应包含libsass 所在的目录 库文件位于。例如,如果是静态库, 那么文件将被称为libsass.a。
    • 从库源码看,它是一个C++库,所以你不会 可以直接在 Swift 中使用;包装器是必要的, 例如,用 Objective-C++ 编写。

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 2020-11-30
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      相关资源
      最近更新 更多