[R] how to link C code with gsl from R CMD and dyn.load

Hao Cen hcen at andrew.cmu.edu
Fri Nov 20 02:29:52 CET 2009


Hi,

I am writing a function in C that is meant to be called by R. In the C
function, I used a gsl function gsl_stats_mean. The code is as simple as
below

void gsl(double *m, int *dim){
	int r, c;
	r = dim[0];
	c = dim[1];

	double mean = gsl_stats_mean(&m[0], 1, r);
	Rprintf("mean = %f\n", mean);
}


The C code is succesfully compiled and the output is as follows.
$ R CMD SHLIB hello.c
WARNING: ignoring environment value of R_HOME
gcc -std=gnu99 -I/usr/local/R-2.8.1/lib64/R/include  -I/usr/local/include 
  -fpic  -g -O2 -c hello.c -o hello.o
gcc -std=gnu99 -shared -L/usr/local/lib64 -o hello.so hello.o

However, as I tried to load it in R

libPath = "~/ccode"
dyn.load(file.path(libPath, "hello.so"))

I got an error message in R

Error in dyn.load(file.path(libPath, "hello.so")) :
  unable to load shared library '~/ccode/hello.so':
  ~/ccode/hello.so: undefined symbol: gsl_stats_mean

I do have gsl installed and the gls lib folder has both libgsl.a and
libgsl.so.

How I can link the C code with gsl library from R CMD SHLIB?

Any suggestions would be appreciated.

Jeff




More information about the R-help mailing list