[Rd] include libraries for C code called from R

braunm at MIT.EDU braunm at MIT.EDU
Tue Dec 12 21:50:50 CET 2006

I hope that someone on this list can help me with this issue.  I have searched
through all of the documentation and mail archives for a solution, but have
been unable to find an answer that addresses my particular problem directly.

Suppose I am writing some C code that I want to access from R through the .C
function.  As part of the C function, I want to include an external C library,
such as the GNU gsl library.  So, if I want to compute the value of a Gaussian
hypergeometric function, my C code would be:

#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 then compile this file (successfully, on a Linux machine with gcc installed)
using R CMD SHLIB gslTest.c.  The message from the complier is:

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

When I go into R and enter the command dyn.load("gslTestHG.so"), I get the
following error:

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:

So, it looks like R is not recognizing the function call within my own C
function, even though the library was included.

Note that this is a simpler example of my "real" problem, the details of which
would bog down discussion.  I know that I could use the gsl package, or include
the Rmath.h library, to perform this particular task.  My primary need is the
ability to call C libraries from wtihin my own C code.

But I would greatly appreciate any advice or suggestions on how to solve this
problem.  I am a novice C programmer and Linux user, but it seems like this
should be a simple fix.

Many thanks in advance,

Michael Braun
MIT Sloan School of Management
braunm at mit.edu

More information about the R-devel mailing list