Home > Enterprise >  C 17 in a package with Rcpp
C 17 in a package with Rcpp

Time:07-16

#include <cmath>
#include <math.h>
#include <Rcpp.h>

// [[Rcpp::plugins("cpp17")]]
// [[Rcpp::export]]
double rcpp_hello_world(Rcpp::NumericVector x) {
  
  double z;
  z = std::cyl_bessel_i(0, x[0]);
  return z ;
}

When I run the above code and call it using sourceCpp, it works as expected. However inside a R package setup I get

error: cyl_bessel_iis not a member of ‘std
      11 |         z = std::cyl_bessel_i[0]);
         |                  ^~~~~~~~~~~~
   make: *** [/usr/lib64/R/etc/Makeconf:177: rcpp_hello_world.o] Error 1
   ERROR: compilation failed for package ‘Package’

upon running Rcpp::compileAttributes() and devtools::document(). I initialised the package with Rcpp.package.skeleton.

cyl_bessel_i was added to the standard library in C 17.

I am using gcc-12.1.0-2 and R-4.2.0-3. I am on Arch Linux.

CodePudding user response:

The default C standard used in current versions of R is C 11 (or C 14 if available). Since you require C 17, you need to declare it in the src/Makevars file, using the line

CXX_STD=CXX17

In a comment you referred to the Rcpp documentation which says that Makevars is optional since Rcpp 0.11.0. I think that was written about linking to the Rcpp libs, but it is also true here, as pointed out by @MikkoMarttila: you can alternatively declare the C version in the SystemRequirements: field of the DESCRIPTION file, e.g.

SystemRequirements: C  17
  • Related