我正在尝试将 libsass (一个 C 库)导入到我用 Swift 编写的 OSX 项目中。但不知怎的,我正在处理很多依赖性问题。顺便说一句,我在本指南中使用模块映射方法: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 编译器 > 搜索路径 > 将路径导入到“${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 build设置 > 搜索路径 > header 搜索路径设置为“$(PROJECT_DIR)/libsass/include”
build设置 > 搜索路径 > 库搜索路径设置为“$(PROJECT_DIR)/libsass/include”
这次我没有看到以前的错误,但在 test_superselector.cpp 中遇到了一个新错误:
'../ast.hpp' file not found
6 我将“-lsass”添加到“build设置”>“链接”>“其他链接器标志”。因为在 libsass 提供的示例中,他们使用 -lsass 标志来编译 c 代码。
同样的,不好。
我花了两天时间试图解决这个问题,但没有明显的进展。请查看 libsass 存储库。任何帮助表示赞赏。 谢谢!!
最佳答案
我简单浏览了一下github上的libsass代码。找不到 ast.hpp
文件,因为您必须已将 libsass 源目录添加到 Xcode 项目中,并且它正在尝试将库源构建为项目的一部分。
由于您将 -lsass
添加到链接器标志中,我认为您正在尝试在项目中使用已构建的库,这是一种更简洁的方法,这就是模块映射的有用之处。
您不应将 libsass 目录添加到您的 Xcode 项目中。您可以将包含 libsass header 和库的目录放置在项目目录中,这与将它们添加到项目中不同。
module.map
中的 header
行应具有 header 的绝对路径或相对于 module.map
位置的路径。
其他一些观察结果:
- 库搜索路径应包含
libsass
所在的目录 库文件位于。例如,如果是静态库, 那么该文件将被称为libsass.a
。 - 从库源码来看,它是一个C++库,所以你不会 能够直接在 Swift 中使用它; wrapper 是必要的, 例如,用 Objective-C++ 编写。
关于ios - 将 C 库导入 Swift 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37847734/