[Rd] include libraries for C code called from R

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Dec 12 23:37:32 CET 2006


Just a note: if you do need -L, it would almost certainly be -L/usr/lib64
on this system.

As a diagnostic check, try

nm -g /mnt/san2/braunm/winshare/braunm/Cpractice/RgslTest.so

which will probably have 'U' for lots of symbols from gsl.

On Tue, 12 Dec 2006, Duncan Temple Lang wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> 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
> gslTestHG.so, you are not telling the linker to combine information
> from the libgsl.so 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_CPPFLAGS=-I/usr/include
>  PKG_LIBS=-L/usr/lib -lgsl
>
> The second of these lines tells the linker to include the symbols in
> libgsl.so 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.
>
>
> D.
>
>
> braunm at 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.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:
>> 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
>> braunm at mit.edu
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> - --
> Duncan Temple Lang                    duncan at wald.ucdavis.edu
> Department of Statistics              work:  (530) 752-4782
> 4210 Mathematical Sciences Building   fax:   (530) 752-7099
> One Shields Ave.
> University of California at Davis
> Davis,
> CA 95616,
> USA
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.3 (Darwin)
>
> iD8DBQFFfyRR9p/Jzwa2QP4RAvBBAJ9nNDK4kWNt2fpCv9pw/r3XPpq6VQCfU9CL
> ziDU+0OcPTEJgVV1RR/OFvA=
> =oMxN
> -----END PGP SIGNATURE-----
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list