[Rcpp-devel] class methods and R_RegisterCCallable

Leonardo Silvestri lsilvestri at ztsdb.org
Thu Oct 24 18:23:15 CEST 2019


> On 24 October 2019 at 16:01, Serguei Sokol wrote:
> | I'am trying to find a way to access class methods of one Rcpp package 
> | (say pkgM) from C++ code in another one (say pkgA).
> | If it were simple C++ routines and not class methods, the way is known 
> | and even automatized. E.g. if I generate pkgM with
> | 
> | Rcpp::Rcpp.package.skeleton("pkgM", module=TRUE)
> | Rcpp::compileAttributes("pkgM")
> | 
> | then in pkgM/src/RcppExports.cpp we can see
> | 
> | 
> | static const R_CallMethodDef CallEntries[] = {
> |      {"_pkgM_rcpp_hello_world", (DL_FUNC) &_pkgM_rcpp_hello_world, 0},
> | ...
> | 
> | As far as I understand, it registers _pkgM_rcpp_hello_world for use with 
> | R_GetCCallable from other packages. And it is a consequence of preceding
> | rcpp_hello_world() with "// [[Rcpp::export]]".
> | If I add "// [[Rcpp::interfaces(r, cpp)]]" in pkgM/src/rcpp_module.cpp 
> | it produces pkgM/inst/include/{pkgM.h,pkgM_RcppExports.h} and
> | 
> | static const R_CallMethodDef CallEntries[] = {
> | ...
> |      {"_pkgM_RcppExport_registerCCallable", (DL_FUNC) 
> | &_pkgM_RcppExport_registerCCallable, 0},
> | ...
> | but still nothing for methods and functions from RCPP_MODULE.
> | 
> | So my question, what is a canonical way (if defined) to expose class 
> | methods (via RCPP_MODULE or any other mechanism) in such a way that they 
> | become callable from C++ code of another package? Am I supposed to 
> | register them manually in some way using 
> | _pkgM_RcppExport_registerCCallable()? And if so, how can I retrieve them 
> | in C++ code of another package?
> 
>>From the top of my head I would say "No" for the simple reason that all R
> does (and this is the same for all other foreign-function interfaces) has to
> be C based.  Hence no class methods.  C++ signatures are compiler dependent,
> and have been avoided for that reason for as long as we had them.
> 
> So I fear you need plain C wrappers for every member function you want to
> expose. I could of course be wrong -- if someone else has counter-examples I
> would be all ears!
> 
> Dirk
> 
> -- 
> http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org

I believe Dirk is correct in saying the traditional mechanism won't work 
for non-static member functions, but if the C++ code is fully contained 
in header files in 'inst/include', then it's possible to access that 
code from another package since there's no library that one needs to 
link against. I believe that's the mechanism that Rcpp itself uses.

Things get ugly when one tries to link to another package's library. 
I've tried a few experiments with Dirk's help 
(https://github.com/lsilvest/linktest), but haven't found a reliable way 
to make this portable (things break when packages are installed 
pre-compiled).

Leo


More information about the Rcpp-devel mailing list