Re: [Rd] include libraries for C code called from R

From: Duncan Temple Lang <>
Date: Tue 12 Dec 2006 - 21:51:13 GMT

Hash: SHA1

Hi Michael.

 [There's no need to send two pieces of mail that are essentially the same issue.]

 The problem is that when you are creating the shared object, you are not telling the linker to combine information from the or libgsl.a file. The linker needs that to resolve the symbols that were referenced in your RgslTest.c code. Without this, the linker then assumes that these missing symbols will be found when the .so is loaded, i.e. in the host application, namely R. Compiling, linking and loading are three different steps in this process. It helps to diagnose problems if one has an understanding of what each does (approximately). Otherwise, the error messages can be cryptic.

 One way to fix the problem is to create a file names Makevars in the same directory as RgslTest.c and add to it

  PKG_LIBS=-L/usr/lib -lgsl

The second of these lines tells the linker to include the symbols in or libgsl.a and that it should look for this in /usr/lib/ (You may have to change this, but it looks like that is where you have gsl installed juding from the #include). The -I/usr/include and -L/usr/lib are likely to be unnecessary but illustrate the idea that one adds library-specific include and library directories to allow the compiler and linker respectively find files it needs for that library's code.

And change your RgslTest.c code to have

   #include <gsl/gsl_sf_hyperg.h>
not the full path name to the .h file.


braunm@MIT.EDU wrote:
> 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.o -L/usr/lib64/R/lib -lR
> When I go into R and enter the command dyn.load(""), 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/':
> /mnt/san2/braunm/winshare/braunm/Cpractice/ undefined symbol:
> gsl_sf_hyperg_2F1
> 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
> ______________________________________________
> mailing list

-----END PGP SIGNATURE----- mailing list Received on Wed Dec 13 17:04:19 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 Wed 13 Dec 2006 - 09:30:54 GMT.

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