[Rcpp-devel] Re-using C++ code from a package with modules and defining linker flags using Rscript

Jelmer Ypma jelmerypma at gmail.com
Sun May 15 21:42:40 CEST 2011


Dear all,

This is a question about recommended ways of linking C++ code in an R
package to C++ code defined in other R packages (using Rcpp 0.9.4.1
and Rtools on Windows). I built package A, which contains C++ classes
and uses modules to expose some of the methods in these classes to R.
Then I built package B, which uses some of the C++ classes in A
directly. I added package A to the LinkingTo: and Depends: fields in
DESCRIPTION. In order for R to find the header-files (.h) of package
A, when compiling package B, I put those header files in
A/inst/include. R-Functions LdFlags() and CxxFlags() are defined in
package A, so I can have something like

PKG_LIBS = $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e
"Rcpp:::LdFlags()") \
           $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "A:::LdFlags()")

in B/src/Makevars.

At this point I ran into problems when installing package B (error:
failed to load module modA from package A). It turns out the same
problem occurs when using a Rcpp skeleton package with modules.

library('Rcpp')
Rcpp.package.skeleton( 'testmodA', module=TRUE )

with one file (e.g. inline.R) added in testmodA/R containing
===
LdFlags <- function(print=TRUE) {
    if (print) cat("test\n") else return( "test" )
}
===

Install the package
R CMD INSTALL testmodA

Including
PKG_LIBS = $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e
"Rcpp:::LdFlags()") \
           $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e
"testmodA:::LdFlags()")

in package B's Makevars leads to errors during compilation, which I
traced back to:
> Rscript -e "testmodA:::LdFlags()"

Error: .onLoad failed in loadNamespace() for 'testmodA', details:
  call: value[[3L]](cond)
  error: failed to load module yada from package testmodA
Execution halted

If I replace this by
Rscript -e "library(testmodA); testmodA:::LdFlags()"
Loading required package: methods
Loading required package: Rcpp
test

it works as expected, so this is what I use in the Makevars file of package B.

Without modules in testmodA
> Rscript -e "testmodA:::LdFlags()"
works fine.

Is this the recommended way of adding linker and cxx flags to packages
depending on other R packages with modules? I tried to look at
packages using RcppArmadillo or RcppGSL for inspiration, but both of
these packages don't implement modules. Any other packages I should
look at?

Many thanks in advance,
Jelmer


More information about the Rcpp-devel mailing list