<p><br>
On May 15, 2011 5:17 PM, "Dirk Eddelbuettel" <<a href="mailto:edd@debian.org">edd@debian.org</a>> wrote:<br>
><br>
><br>
> Hi Jelmer,<br>
><br>
> On 15 May 2011 at 21:42, Jelmer Ypma wrote:<br>
> | Dear all,<br>
> |<br>
> | This is a question about recommended ways of linking C++ code in an R<br>
> | package to C++ code defined in other R packages (using Rcpp 0.9.4.1<br>
><br>
> Well, you seem to make the assumption that this would be supported and<br>
> documented. But it is not really.<br>
><br>
> | and Rtools on Windows). I built package A, which contains C++ classes<br>
> | and uses modules to expose some of the methods in these classes to R.<br>
> | Then I built package B, which uses some of the C++ classes in A<br>
> | directly. I added package A to the LinkingTo: and Depends: fields in<br>
> | DESCRIPTION. In order for R to find the header-files (.h) of package<br>
> | A, when compiling package B, I put those header files in<br>
> | A/inst/include. R-Functions LdFlags() and CxxFlags() are defined in<br>
> | package A, so I can have something like<br>
> |<br>
> | PKG_LIBS = $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e<br>
> | "Rcpp:::LdFlags()") \<br>
> | $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "A:::LdFlags()")<br>
> |<br>
> | in B/src/Makevars.<br>
> |<br>
> | At this point I ran into problems when installing package B (error:<br>
> | failed to load module modA from package A). It turns out the same<br>
> | problem occurs when using a Rcpp skeleton package with modules.<br>
> |<br>
> | library('Rcpp')<br>
> | Rcpp.package.skeleton( 'testmodA', module=TRUE )<br>
> |<br>
> | with one file (e.g. inline.R) added in testmodA/R containing<br>
> | ===<br>
> | LdFlags <- function(print=TRUE) {<br>
> | if (print) cat("test\n") else return( "test" )<br>
> | }<br>
> | ===<br>
> |<br>
> | Install the package<br>
> | R CMD INSTALL testmodA<br>
> |<br>
> | Including<br>
> | PKG_LIBS = $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e<br>
> | "Rcpp:::LdFlags()") \<br>
> | $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e<br>
> | "testmodA:::LdFlags()")<br>
> |<br>
> | in package B's Makevars leads to errors during compilation, which I<br>
> | traced back to:<br>
> | > Rscript -e "testmodA:::LdFlags()"<br>
> |<br>
> | Error: .onLoad failed in loadNamespace() for 'testmodA', details:<br>
> | call: value[[3L]](cond)<br>
> | error: failed to load module yada from package testmodA<br>
> | Execution halted<br>
> |<br>
> | If I replace this by<br>
> | Rscript -e "library(testmodA); testmodA:::LdFlags()"<br>
> | Loading required package: methods<br>
> | Loading required package: Rcpp<br>
> | test<br>
> |<br>
> | it works as expected, so this is what I use in the Makevars file of package B.<br>
> |<br>
> | Without modules in testmodA<br>
> | > Rscript -e "testmodA:::LdFlags()"<br>
> | works fine.<br>
> |<br>
> | Is this the recommended way of adding linker and cxx flags to packages<br>
> | depending on other R packages with modules? I tried to look at<br>
><br>
> We simply show no example of definiting object code in one package and having<br>
> other packages use it -- with the exception of the Rcpp package itself. And<br>
> there it takes quite some effort to make this happen.<br>
><br>
> You could look at what lme4 and Matrix do; this is documented in the 'Writing<br>
> R Extensions' manual under '[Section] 5.4 Registering native routines'.</p>
<p>Unfortunately that approach doesn't work with C++ classes and methods, which is one of the reasons that Rcpp linkage has to be done the way that it is. The exporting of symbols a as done by Matrix is restricted to C function entry points.</p>
<p>> | packages using RcppArmadillo or RcppGSL for inspiration, but both of<br>
> | these packages don't implement modules. Any other packages I should<br>
> | look at?<br>
><br>
> RcppArmadillo is very different: it used templates only, no linking. RcppGSL<br>
> uses GSL; for that we standard configure and system GSL libraries. None of<br>
> these package take anything from another R package, besides Rcpp.<br>
><br>
> So as a simpler step I would just include package A's code in package B, or<br>
> make it one package.<br>
><br>
> Sorry, Dirk<br>
><br>
> --<br>
> Gauss once played himself in a zero-sum game and won $50.<br>
> -- #11 at <a href="http://www.gaussfacts.com">http://www.gaussfacts.com</a><br>
> _______________________________________________<br>
> Rcpp-devel mailing list<br>
> <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</p>