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