[R] problems linking c++ code
Prof Brian Ripley
ripley at stats.ox.ac.uk
Mon Sep 10 10:33:39 CEST 2001
On 9 Sep 2001, Allan Strand wrote:
>
> I've been trying to learn about dynamically loading subroutines in R.
> Ultimately I would like to use this approach to implement some
> simulations. For the moment though, I've run into a problem compiling
> c++ code and loading it into R.
>
> The 'C/C++' and 'R'code below works fine when compiled and linked with
> gcc, but gives an error when the library is compiled and linked with
> g++. In the case when it fails, it does so because of an undefined
> symbol in the *.so file. Is there something I can link in to resolve
> this problem? Is there another obvious solution?
Using the Rinternals.h in C++ code is not supported.
It's not clear to me why you are writing C in C++ inside extern "C"
declarations, but if you do you need to put
the Rinternals.h inside extern "C" too.
BTW, you code is not using Rdefines.h, but I think that may need to be
inside extern "C" too.
The `standard' headers (those used with .C) have extern "C" wrappers, but
not the advanced ones for .Call/.Extern. We assume that people using
.Call are pretty au fait with their ccompiler systems, and already know
how to use .C.
>
> The system is FreeBSD 4.x-stable. The R version is 1.3.1. The gcc/g++
> version is 2.95.3. I compile with R CMD INSTALL and my MAkevars file
> has the following lines:
>
> PKG_CXXFLAGS = -fPIC -I./ -I../../
> PKG_CFLAGS = -fPIC -I./ -I../../
> PKG_LIBS = -L/usr/local/lib
>
>
> The error I get when loading the library in R is:
> > library(rmetasim)
> Error in dyn.load(x, as.logical(local), as.logical(now)) :
> unable to load shared library "/usr/local/lib/R/library/rmetasim/libs/rmetasim.so":
> /usr/local/lib/R/library/rmetasim/libs/rmetasim.so: Undefined symbol "Rf_unprotect__Fi"
> Error in library(rmetasim) : .First.lib failed
> >
>
>
> R-code
> =====================
>
> .First.lib <- function(libpath, libname)
> {
> library.dynam(libname)
> }
That should be library.dynam("rmetasim", libname, libpath) to make sure
you use your package.
>
> read.landscape <- function(fn = "filename")
> {
> .Call("read_landscape",fn,PACKAGE = "rmetasim")
> }
>
> C-code
> ===================
> #include <R.h>
> #include <Rdefines.h>
> #include <Rinternals.h>
>
> #ifdef __cplusplus
> extern "C" SEXP read_landscape(SEXP fn);
> extern "C" {
> #endif
>
> SEXP read_landscape(SEXP fn)
> {
> SEXP l;
> PROTECT(l = allocVector (REALSXP, 2));
> REAL(l)[0] = 12.0;
> REAL(l)[1] =14.5;
> UNPROTECT(1);
> return l;
> }
>
> #ifdef __cplusplus
> } ///end of extern "C"
> #endif
>
> --
> Allan Strand, Biology
> College of Charleston
> Charleston, SC 29424
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
--
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 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list