Reputation: 159
I am writing a C code that I will use in R. To do matrix operations in C, I added "gsl_matrix" library. When I compile using R CMD SHLIB it compiles without problem. However when I open R and try to write dyn.load("file.so"), I receive an error message:
unable to load shared object file.so
undefined symbol: gsl_matrix_alloc
Where is my mistake?
Upvotes: 1
Views: 1979
Reputation: 29347
I suspect this has to do with your shared library that is not properly linked to GSL libs, as discussed on R-devel, or the manual on Writing R Extensions, where it is suggested to use a Makevars
file (with something like PKG_LIBS=-L/usr/lib -lgsl
). Otherwise, following the example in help(SHLIB)
, you may want to try:
$ R CMD SHLIB file.c -lgsl -lgslcblas
There is a simple tutorial, R Call GSL, which shows basic setup for calling GSL functions.
I am able to reproduce the toy example, that I renamed nperms.{c,r}
as follows (on a Mac, whence the use of a -dynamiclib
switch in place of -shared
):
~/scratch $ gcc -c nperms.c
~/scratch $ file nperms.o
nperms.o: Mach-O 64-bit object x86_64
~/scratch $ gcc -dynamiclib -lgsl -lgslcblas -o libnperms.dylib -dylib nperms.o
~/scratch $ ls *nperm*
libnperms.dylib nperms.c nperms.o
~/scratch $ file libnperms.dylib
libnperms.dylib: Mach-O 64-bit dynamically linked shared library x86_64
Everything works fine when dyn.load
'ing libnperms.dylib
in R. However, using shared library generated from R CMD SHLIB
without further argument
~/scratch $ R CMD SHLIB nperms.c
gcc -arch x86_64 -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -o nperms.so nperms.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
~/scratch $ ls *nperm*
libnperms.dylib nperms.c nperms.o nperms.r nperms.so
~/scratch $ file nperms.so
nperms.so: Mach-O 64-bit dynamically linked shared library x86_64
raises the following error (sorry for the French locale)
> dyn.load("nperms.so")
Erreur dans dyn.load("nperms.so") :
impossible de charger l'objet partag'e '/Users/chl/scratch/nperms.so':
dlopen(/Users/chl/scratch/nperms.so, 6): Symbol not found: _gsl_permutation_alloc
Referenced from: /Users/chl/scratch/nperms.so
Expected in: flat namespace
in /Users/chl/scratch/nperms.so
Upvotes: 2
Reputation: 2366
I have not a direct answer to your question but did you try http://cran.r-project.org/web/packages/RcppGSL/index.html or http://cran.r-project.org/web/packages/RcppArmadillo/index.html?
Upvotes: 0