From murdoch at stats.uwo.ca  Tue Dec 12 22:46:02 2006
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Tue, 12 Dec 2006 16:46:02 -0500
Subject: [Rd] Undefined symbol when trying to dyn.load a shared object
In-Reply-To: <20061212160335156.00000003624@braunm1>
References: <20061212160335156.00000003624@braunm1>
Message-ID: <457F231A.8050304@stats.uwo.ca>

On 12/12/2006 4:03 PM, Michael Braun wrote:
> I hope that someone reading this list can offer some suggestions on how I can incorporate external C libraries in C functions that I want to call from R.  I have gone through all of the documentation, help files and mailing list archives and have not been able to find a satisfactory solution.
> 
> I have constructed a simple example of the problem, since my "real" program is much more complex and would bog down discussion.  Suppose I want to compute the value of a Gaussian hypergeometric function using the function in the GSL library.  My C code is:
> 
> 	#include </usr/include/gsl/gsl_sf_hyperg.h>
> 
> 	void gslTestHG (double *a, double *b, double *c, double *x, double *val ) {
> 		*val = gsl_sf_hyperg_2F1(*a,*b,*c,*x); 
> 	}
> 
> I compile this code (successfully) using R CMD SHLIB gslTest.c and get the following result
> 
> 	-bash-3.00$ R CMD SHLIB RgslTest.c
> 	gcc -I/usr/lib64/R/include -I/usr/lib64/R/include  -I/usr/local/include   -fpic  -O2 -g -c RgslTest.c -o RgslTest.o
> 	gcc -shared -L/usr/local/lib64 -o RgslTest.so RgslTest.o   -L/usr/lib64/R/lib -lR
> 
> Next, I try to load the shared object into R using dyn.load("RgslTest.so") and get the following error in R:
> 
> 	> dyn.load("RgslTest.so")
> 	Error in dyn.load(x, as.logical(local), as.logical(now)) : 
>         unable to load shared library '/mnt/san2/braunm/winshare/braunm/Cpractice/RgslTest.so':
>  	 /mnt/san2/braunm/winshare/braunm/Cpractice/RgslTest.so: undefined symbol: gsl_sf_hyperg_2F1
> 
> I am certain that the file path in the #include statement is correct.  It looks like R can't find the gsl function in the C program, even though the compiler did.

The #include statement just includes the headers, so that calls to the 
functions are constructed properly by the compiler.  It doesn't pull in 
the library.  That's done at link time.

You need at least a Makevars file which defines PKG_LIBS to tell the 
linker where to find the external library.  That macro should contain 
linker options -l and -L that point out the need for gsl, and where to 
find it. If you want to distribute this package to others, you'll 
probably need a configure script too, in order to construct those values.

Duncan Murdoch

> I would greatly appreciate any help you can provide.  Many thanks in advance.
> 
> Best wishes,
> 
> Michael
> 
> ------------------------------------------
> Michael Braun
> Assistant Professor of Marketing
> MIT Sloan School of Management
> 38 Memorial Drive, E56-329
> Cambridge, MA 02139
> braunm at mit.edu
> (617) 253-3436
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] Undefined symbol when trying to dyn.load a shared object
Duncan Murdoch