linux - 在 Swift (linux) 中链接 C 库及其支持库

标签 linux swift gsl

我想在 Swift 中使用 GNU 科学库,特别是我想使用 gsl_rng.h 中的例程。所以我基本按照https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md (我使用的是 Linux,而不是 OS X)

我将模块创建为

module CGSL [system] {
  header "/usr/include/gsl/gsl_rng.h"
  link "gsl"
  export *
}

但是,我无法构建我的程序,因为我收到了很多类似undefined reference to 'cblas_dasum' 的消息。事实上,正如 GSL 的文档所述

To link against the library you need to specify both the main library and a supporting CBLAS library, which provides standard basic linear algebra subroutines. A suitable CBLAS implementation is provided in the library libgslcblas.a if your system does not provide one. The following example shows how to link an application with the library,

$ gcc -L/usr/local/lib example.o -lgsl -lgslcblas -lm

我应该如何链接 -lgsl-lgslcblas

最佳答案

libgslcblas 添加第二个 link 行就可以了:

module CGSL [system] {
  header "/usr/include/gsl/gsl_rng.h"
  link "gsl"
  link "gslcblas"
  export *
}

您可能还需要添加 link "m",即使我不必在我的盒子上这样做 (Ubuntu 14.04)。

我没有在 Swift 文档中找到关于此的具体建议,不得不做出有根据的猜测,但它奏效了。 Linux 上的 Swift 正在进行中,包管理器仅适用于 Swift 3.0 开发快照,Swift 3.0 是一种不稳定的、积极开发的语言的最新版本。这种常见场景没有得到很好的记录这一事实应该让您了解该技术的成熟度。

作为包管理器的替代方案,您可能需要考虑使用桥接 header ,如该问题的答案中所述: Compile C code and expose it to Swift under Linux .

无论采用哪种方式,更大的挑战将是从 Swift 调用 GSL API,因为该 API 使用了大量非原始类型。要解决该问题,请考虑编写一个 C 包装器,该包装器具有可以从 Swift 轻松调用的简化接口(interface)。然后可以使用桥接头或系统模块调用包装器。

关于linux - 在 Swift (linux) 中链接 C 库及其支持库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37681331/

相关文章:

java - 加载共享库时出错 : libcrfpp. so.0

linux - libs 目录不会在 libtool 中自动创建

c - 映射文件在读取时不占用内存,但在写入时占用内存

ios - 无法将类型 'QuerySnapshot?' 的值转换为预期的参数类型 'DataSnapshot'

ios - 如何追加数组。或者我如何将模型转换为 nsmuttable 数组

c++ - 在 Windows 中构建 GSL(GNU 科学图书馆)以与 VS2015 一起使用

android - dlopen 失败 : cannot locate symbol "cblas_sdsdot" referenced by "libgsl.so"

python - 如果文件 1 中的 A 列 = 文件 2 中的 A 列,则替换为文件 2 中的 B 列

linux - 是否可以在 Swift 中(在 Linux 下)使用 GNU 科学图书馆?

r - R 中的 GSL 库 - 未找到符号