在Swift(Linux)中链接C库及其支持库 [英] Linking a C library and its supporting library in Swift (linux)

查看:93
本文介绍了在Swift(Linux)中链接C库及其支持库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use GNU scientific library in Swift, particularly I want to use routines from gsl_rng.h. So I basically follow https://github.com/apple/swift-package-manager/blob/master/Documentation/SystemModules.md (I am using Linux, not OS X)

我将模块创建为

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

但是,由于收到很多undefined reference to 'cblas_dasum'类型的消息,所以我无法构建程序.实际上,正如GSL的文档所述

However, I can't build my program, since I get lots of messages of the kind undefined reference to 'cblas_dasum'. And indeed, as the documentation for GSL states

要链接到库,您需要同时指定两个主库 和一个支持的CBLAS库,它提供了标准的基本线性 代数子程序.在以下位置提供了合适的CBLAS实现 库libgslcblas.a(如果您的系统未提供).这 以下示例显示了如何将应用程序与库链接,

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?

What shall I do to link both -lgsl and -lgslcblas?

推荐答案

libgslcblas添加第二个link行将达到目的:

Adding a second link line for libgslcblas will do the trick:

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

您可能还需要添加link "m",即使我不必在包装盒中添加link "m"(Ubuntu 14.04).

You may also need to add link "m", even though I didn't have to do that on my box (Ubuntu 14.04).

我在Swift文档中没有找到关于此的具体建议,因此不得不做出有根据的猜测,但是它确实有效. Linux上的Swift正在开发中,并且Package Manager仅适用于Swift 3.0开发快照,Swift 3.0是不稳定的,积极开发的最新版本.这样的常见情况没有得到充分记录的事实应该使您对该技术的成熟有所了解.

I did not find a specific advice on this in Swift documentation and had to make an educated guess, but it worked. Swift on Linux is work in progress and the Package Manager is only available with Swift 3.0 development snapshots, Swift 3.0 being an unstable, actively developed latest version of the language. The very fact that such a common scenario is not well documented should give you an idea of the technology's maturity.

作为包管理器的替代方案,您可能要考虑使用桥接标头,如对此问题的回答中所述: 编译C代码并将其公开给Swift在Linux下.

As an alternative to the Package Manager you might want to consider using a bridging header as described in an answer to this question: Compile C code and expose it to Swift under Linux.

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

Whichever way you do it, a bigger challenge will be calling the GSL API from Swift because the API uses a lot of non-primitive types. To work around that problem consider writing a C wrapper with a simplified interface that can be easily called from Swift. The wrapper can then be called by using a bridging header or system modules.

这篇关于在Swift(Linux)中链接C库及其支持库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆