[Rd] LinkingTo and C++

Romain Francois romain at r-enthusiasts.com
Thu Feb 11 10:08:38 CET 2010


I've been trying to make LinkingTo work when the package linked to has 
c++ code.

I've put dumb packages to illustrate this emails here ; 

Package A defines this C++ class:

class A {
	A() ;
	~A() ;
	SEXP hello() ;
} ;

Package B has this function :

SEXP say_hello(){
	A a ;
	return a.hello() ;

headers of package A are copied into inst/include so that package B can 

LinkingTo: A

in its DESCRIPTION file.

Also, package B has the R function ;

g <- function(){
	.Call("say_hello", PACKAGE = "B")

With this I can compile A and B, but then I get :

$ Rscript -e "B::g()"
Error in dyn.load(file, DLLpath = DLLpath, ...) :
   unable to load shared library '/usr/local/lib/R/library/B/libs/B.so':
   /usr/local/lib/R/library/B/libs/B.so: undefined symbol: _ZN1AD1Ev
Calls: :: ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>

If I then add a Makevars in B with this :

# find the root directory where A is installed
ADIR=$(shell $(R_HOME)/bin/Rscript -e "cat(system.file(package='A'))" )


Then it works:

$ Rscript -e "B::g()"
[1] "hello"

So it appears that adding the -I flag, which is what LinkingTo does is 
not enough when the package "linking from" (B) actually has to link to 
the "linked to" package (A).

I've been looking at 
but it seems only applicable to c(++) functions and not classes ...

What am I missing ? Should/can linkingto be extended in a way that 
accomodates c++


Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
|- http://tr.im/NrTG : Rcpp 0.7.5
|- http://tr.im/MPYc : RProtoBuf: protocol buffers for R
`- http://tr.im/KfKn : Rcpp 0.7.2

More information about the R-devel mailing list