سؤال

I am trying to compile my boost/regrex-based library function in R. This is my example code:

// [[Rcpp::depends(BH)]]
#include <Rcpp.h>
#include <boost/regex.hpp>

using namespace Rcpp;  
using namespace boost;
using namespace std;

string jphtest(string);

// [[Rcpp::export]]
string jphtest(string src) {

        return(regex_replace(src, regex("ABC"), "123"));
}

When I attempt to test this with R CMD check, it doesn't work. Here's the error I get:

** testing if installed package can be loaded
Error in dyn.load(file, DLLpath = DLLpath, ...) : 
  unable to load shared object '/Users/howardjp/Documents/Research/foniks/..Rcheck/foniks/libs/foniks.so':   
dlopen(/Users/howardjp/Documents/Research/foniks/..Rcheck/foniks/libs/foniks.so, 6): Symbol not found: __ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9do_assignEPKcS7_j
  Referenced from: /Users/howardjp/Documents/Research/foniks/..Rcheck/foniks/libs/foniks.so
  Expected in: flat namespace in /Users/howardjp/Documents/Research/foniks/..Rcheck/foniks/libs/foniks.so
Error: loading failed
Execution halted

Any help on how to correct this is appreciated.

هل كانت مفيدة؟

المحلول

The BH package very conveniently packages a large part of Boost in a header-only library you can simply use in R via LinkingTo: or our plugin.

However, there are parts of Boost for which you need to link as well and regex is one them. Filesystem, threads, program_options, ... are other examples. You can do that either in a package, or via an explicit linking instruction as i do in the Rcpp Gallery article that Gabor already pointed too. Note that that solution is not entirely portable; you would need a package to link to the right file extension on Windows, OS X, Linux etc.

In short, your dismissal of Gabor's comment is wrong as you seem to have overlooked that Boost Regex is not header-only.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top