Search code examples
c++rpackagercpp

R package with Rcpp


I tried to add a small C++ function (called reduceString) into an R package of mine using Rcpp but I failed to configurate the package so that it compiles fine. The package can be found here.

devtools::install_github("RemiMattheyDoeret/SimBitWrapper")

Here is part of the error message

Error: package or namespace load failed for ‘SimBitWrapper’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/SimBitWrapper/libs/SimBitWrapper.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/SimBitWrapper/libs/SimBitWrapper.so, 6): Symbol not found: __Z12reduceStringv
  Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/SimBitWrapper/libs/SimBitWrapper.so
  Expected in: flat namespace
 in /Library/Frameworks/R.framework/Versions/3.5/Resources/library/SimBitWrapper/libs/SimBitWrapper.so

I have tried to modify again and again the NAMESPACE and DESCRIPTION files as well as src/RcppExports.cpp and R/RcppExports.r files but I just fail to fix the problem. Here are those files as well as the reduceString.cpp file

NAMESPACE

useDynLib(SimBitWrapper, .registration=TRUE)
exportPattern("^[^\\.]")
importFrom(Rcpp, evalCpp)

DESCRIPTION

Package: SimBitWrapper
Title: R wrapper for SimBit
Version: 5.1.0
Authors@R: 
    person(given = "Remi",
           family = "Matthey-Doret",
           role = c("aut", "cre"),
           email = "remi.b.md@gmail.com",
           comment = c(ORCID = "0000-0001-5614-5629"))
Description: The package is a wrapper for SimBit (SimBit is a simulation platform for evolutionary genetic studies). Please see the SimBit manual (at https://github.com/RemiMattheyDoret/SimBit) for description of how to use this SimBitWrapper package.
License: MIT
Encoding: UTF-8
LazyData: true
Depends: R (>= 2.15)
Imports: data.table, processx, Rcpp
LinkingTo: Rcpp
Suggests:
URL: https://github.com/RemiMattheyDoret

src/RcppExports.cpp

#include <Rcpp.h>

using namespace Rcpp;

// reduceString
std::string reduceString();
RcppExport SEXP _SimBitWrapper_reduceString() {
BEGIN_RCPP
    Rcpp::RObject rcpp_result_gen;
    Rcpp::RNGScope rcpp_rngScope_gen;
    rcpp_result_gen = Rcpp::wrap(reduceString());
    return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
    {"_SimBitWrapper_reduceString", (DL_FUNC) &_SimBitWrapper_reduceString, 0},
    {NULL, NULL, 0}
};

RcppExport void R_init_SimBitWrapper(DllInfo *dll) {
    R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
    R_useDynamicSymbols(dll, FALSE);
}

R/RcppExports.r

reduceString <- function(x) {
    .Call(`_SimBitWrapper_reduceString`,x)
}

reduceString.cpp

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
std::string reduceString(CharacterVector& x)
{
    // I cut off this part that does not need to be printed here
}

Solution

  • Thanks for posting a link to a repo.

    It worked for me as soon as I re-recreated RcppExports.{cpp,R} using my (current) version of Rcpp and a call to compileAttributes(). What version of Rcpp do you have?

    The log below uses my wrappers from littler but that is immaterial. The R CMD ... commands would have worked the same way.

    Log

    edd@rob:/tmp/SimBitWrapper(master)$ build.r 
    * checking for file ‘./DESCRIPTION’ ... OK
    * preparing ‘SimBitWrapper’:
    * checking DESCRIPTION meta-information ... OK
    * cleaning src
    * checking for LF line-endings in source and make files and shell scripts
    * checking for empty or unneeded directories
    * building ‘SimBitWrapper_5.1.0.tar.gz’
    
    edd@rob:/tmp/SimBitWrapper(master)$ install2.r -l /tmp/lib SimBitWrapper_5.1.0.tar.gz 
    * installing *source* package ‘SimBitWrapper’ ...
    ** using staged installation
    ** libs
    ccache g++ -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/Rcpp/include'    -fpic  -g -O3 -Wall -pipe -pedantic  -c RcppExports.cpp -o RcppExports.o
    ccache g++ -I"/usr/share/R/include" -DNDEBUG  -I'/usr/local/lib/R/site-library/Rcpp/include'    -fpic  -g -O3 -Wall -pipe -pedantic  -c reduceString.cpp -o reduceString.o
    reduceString.cpp: In function ‘std::string reduceString(Rcpp::CharacterVector&)’:
    reduceString.cpp:10:27: warning: comparison of integer expressions of different signedness: ‘size_t’ {aka ‘long unsigned int’} and ‘R_xlen_t’ {aka ‘long int’} [-Wsign-compare]
       10 |     for (size_t i = 0 ; i < x.size() ; ++i)
          |                         ~~^~~~~~~~~~
    ccache g++ -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o SimBitWrapper.so RcppExports.o reduceString.o -L/usr/lib/R/lib -lR
    installing to /tmp/lib/00LOCK-SimBitWrapper/00new/SimBitWrapper/libs
    ** R
    ** byte-compile and prepare package for lazy loading
    ** help
    No man pages found in package  ‘SimBitWrapper’ 
    *** installing help indices
    ** building package indices
    ** testing if installed package can be loaded from temporary location
    ** checking absolute paths in shared objects and dynamic libraries
    ** testing if installed package can be loaded from final location
    ** testing if installed package keeps a record of temporary installation path
    * DONE (SimBitWrapper)
    edd@rob:/tmp/SimBitWrapper(master)$ 
    

    Diff

    edd@rob:/tmp/SimBitWrapper(master)$ git diff
    diff --git a/R/RcppExports.R b/R/RcppExports.R
    index 19a82b4..62f890a 100644
    --- a/R/RcppExports.R
    +++ b/R/RcppExports.R
    @@ -2,6 +2,6 @@
     # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
     
     reduceString <- function(x) {
    -    .Call(`_SimBitWrapper_reduceString`,x)
    +    .Call(`_SimBitWrapper_reduceString`, x)
     }
     
    diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp
    index 90dda3b..0b91977 100644
    --- a/src/RcppExports.cpp
    +++ b/src/RcppExports.cpp
    @@ -6,18 +6,19 @@
     using namespace Rcpp;
     
     // reduceString
    -std::string reduceString();
    -RcppExport SEXP _SimBitWrapper_reduceString() {
    +std::string reduceString(CharacterVector& x);
    +RcppExport SEXP _SimBitWrapper_reduceString(SEXP xSEXP) {
     BEGIN_RCPP
         Rcpp::RObject rcpp_result_gen;
         Rcpp::RNGScope rcpp_rngScope_gen;
    -    rcpp_result_gen = Rcpp::wrap(reduceString());
    +    Rcpp::traits::input_parameter< CharacterVector& >::type x(xSEXP);
    +    rcpp_result_gen = Rcpp::wrap(reduceString(x));
         return rcpp_result_gen;
     END_RCPP
     }
     
     static const R_CallMethodDef CallEntries[] = {
    -    {"_SimBitWrapper_reduceString", (DL_FUNC) &_SimBitWrapper_reduceString, 0},
    +    {"_SimBitWrapper_reduceString", (DL_FUNC) &_SimBitWrapper_reduceString, 1},
         {NULL, NULL, 0}
     };
     
    edd@rob:/tmp/SimBitWrapper(master)$ 
    

    PS: You have a second copy of your package in your repo. I just the top-level. Also, you committed the .DS_Store files from your mac which are of no use to R or a repo.