On 19 November 2009 at 20:29, Hao Cen wrote: | 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.
Set the PKG_LIBS variable as you would in src/Makevars for a package, i.e. $ PKG_LIBS="-lgsl -lblas" R CMD SHLIB hello.c maybe all it takes. You may need to add PKG_CPPFLAGS as well if you need -I... for include directories. See the 'R Extensions' manual for more, and / or some of the CRAN packages that wrap GSL code. Dirk -- Three out of two people have difficulties with fractions. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.