Search code examples
rrcppr-package

Exporting C++ code from an R package with Rcpp::interfaces(r, cpp) throws error `PACKAGE_RcppExport_registerCCallable" not available for .Call()`


I'm trying to export C++ functions from my R package 'TreeTools' so they can be called from a different package. I've added the line

// [[Rcpp::interfaces(r, cpp)]]

to src/renumber_tree.cpp, per [https://dirk.eddelbuettel.com/code/rcpp/Rcpp-attributes.pdf / http://r-pkgs.had.co.nz/src.html] and when I compileAttributes() and document() the package, there are indicators that the export has been acknowledged:

  • Relevant header files are added to inst/includes;
  • #include "../inst/include/TreeTools.h" is added to the start of RcppExports.cpp
  • A function RcppExport SEXP _TreeTools_RcppExport_registerCCallable() is added to the end of RcppExports.cpp

Compliation nevertheless fails with

Error in .Call("_TreeTools_RcppExport_registerCCallable", PACKAGE = "TreeTools") : 
  "_TreeTools_RcppExport_registerCCallable" not available for .Call() for package "TreeTools"

Can anyone help me to debug this error? Could it be related to the fact that I have a manually-generated PACKAGE-init.c file?


Solution

  • Thanks, Dirk, for pointing me in the right direction.

    To TreeTools-init.C, I needed to add

    extern SEXP _TreeTools_RcppExport_registerCCallable();
    

    and

    static const R_CallMethodDef callMethods[] = {
      &_TreeTools_RcppExport_registerCCallable, 0},
      {NULL, NULL, 0}
    };
    

    In RcppExports.R, I saw

    # Register entry points for exported C++ functions
    methods::setLoadAction(function(ns) {
        .Call('_TreeTools_RcppExport_registerCCallable', PACKAGE = 'TreeTools')
    })
    

    Replacing the straight quotes around '_TreeTools_RcppExport_registerCCallable' with backticks, i.e.

    `_TreeTools_RcppExport_registerCCallable`
    

    allowed the package to load.

    Of course, these are replaced with straight quotes whenever compileAttributes() is run, so this doesn't feel like a complete solution...