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

3
我希望在Swift中使用GNU科学库,特别是我想使用中的例程。因此,我基本上遵循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

https://developer.apple.com/library/ios/recipes/xcode_help-project_editor/Articles/AddingaLibrarytoaTarget.html - Alexander
1个回答

2

添加第二个link行,针对libgslcblas即可解决问题:

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

您可能还需要添加链接“m”,即使我在我的计算机上(Ubuntu 14.04)不必这样做。
在Swift文档中,我没有找到关于这个问题的具体建议,只能凭借自己的猜测解决问题,但它确实奏效了。Swift在Linux上仍在开发中,软件包管理器仅可用于Swift 3.0开发快照中,Swift 3.0是该语言的最新版本,尚处于不稳定、积极开发的阶段。这种常见情况没有得到很好的记录,这一点应该让您对技术的成熟度有所了解。
作为软件包管理器的替代方案,您可能想考虑使用桥接标头,如此问题描述中的答案所述:Compile C code and expose it to Swift under Linux
无论您选择哪种方式,更大的挑战将是从Swift调用GSL API,因为API使用了许多非原始类型。为了解决这个问题,可以考虑编写一个具有简化接口的C包装器,可以轻松地从Swift中调用该包装器。然后可以使用桥接标头或系统模块来调用该包装器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接