Re: [Rd] Undefined symbol when trying to dyn.load a shared object

From: Duncan Murdoch <>
Date: Tue 12 Dec 2006 - 21:46:02 GMT

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.o -L/usr/lib64/R/lib -lR
> Next, I try to load the shared object into R using dyn.load("") and get the following error in R:
> > dyn.load("")
> Error in dyn.load(x, as.logical(local), as.logical(now)) :
> unable to load shared library '/mnt/san2/braunm/winshare/braunm/Cpractice/':
> /mnt/san2/braunm/winshare/braunm/Cpractice/ 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
> (617) 253-3436
> ______________________________________________
> mailing list
> mailing list Received on Thu Dec 14 12:56:44 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 14 Dec 2006 - 02:30:54 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.