I'm trying to use nim wrapper for GSL math library, but getting C-linking error.
I installed it with brew install gsl && nimble install gsl and then tried to run code below as nim r -d:nimDebugDlOpen play.nim
import gsl/gsl_sf_bessel
echo gsl_sf_bessel_J0(5.0)
and got an error, how can i fix it?
dlopen(libgsl.dylib, 2): Symbol not found: _cblas_caxpy
Referenced from: /usr/local/lib/libgsl.dylib
Expected in: flat namespace
in /usr/local/lib/libgsl.dylib
could not load: libgsl.dylib
Error: execution of an external program failed: '/alex/projects/alien/build/play '
I also tried to use it from C, to check if all the dependencies installed and it works
#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>
int
main (void)
{
double x = 5.0;
double y = gsl_sf_bessel_J0 (x);
printf ("J0(%g) = %.18e\n", x, y);
return 0;
}
Running it as
gcc -Wall -I/usr/local/Cellar/gsl/2.7/include -c play.c
gcc -L/usr/local/lib play.o -lgsl -lgslcblas -lm
./a.out
Just for fun trying to do it with Futhark, pretty straight forward and seems to work (at least on my machine):
import futhark
{.passL: "-lgsl -lgslcblas".}
importc:
path "/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include"
path "/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include-fixed"
path "/usr/include"
"gsl/gsl_sf_bessel.h"
var
x = 5.0
y = gslSfBesselJ0(x)
echo "J=(", x, ") = ", y
Unfortunately you still need to set your include path for your C compiler and the system header files. It's on my TODO list to figure out how to remove that, but the C compiler libraries depend on the compiler Nim is going to use to compile with, so the logic there might be a bit hairy..
Thanks, I tried using libgslcblas.dylib instead of libgsl.dylib but it seems like it can't find it, although it exists and located in same place as libgsl.dylib, in the /usr/local/lib/libgslcblas.dylib.
> ./play.nim
/Users/alex/.cache/nim/play_d/@mplay.nim.c:13:10: fatal error: 'libgslcblas.dylib' file not found
#include "libgslcblas.dylib"
^~~~~~~~~~~~~~~~~~~
1 error generated.
Error: execution of an external compiler program 'clang -c -w -ferror-limit=3 -I/balex/applications/nim-1.4.6/lib -I/alex/projects/alien -o /Users/alex/.cache/nim/play_d/@mplay.nim.c.o /Users/alex/.cache/nim/play_d/@mplay.nim.c' failed with exit code: 1
Finally... I found the missing piece {.passL: "-lgsl".}, working code
{.passL: "-lgsl".}
proc gsl_sf_bessel_J0*(x: cdouble): cdouble {.cdecl, importc: "gsl_sf_bessel_J0", dynlib: "libgsl.dylib".}
echo gsl_sf_bessel_J0(5.0)