Linking a C library and its supporting library in Swift (linux)

拈花ヽ惹草 提交于 2019-11-29 15:14:40
Anatoli P

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 *
}

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

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.

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!