[R-SIG-Mac] Rcpp w/ OS X Mavericks

Simon Urbanek simon.urbanek at r-project.org
Wed Apr 23 15:46:13 CEST 2014


On Apr 23, 2014, at 3:04 AM, Josh EmBree <jembree at ucla.edu> wrote:

> Hi all, 
> 
> I know this issue has come up a lot in the last few months and I’ve searched around for days trying to find a solution. I can’t get my C++ files that use Rcpp to compile in OS X 10.9 (Mavericks). I’ve done the following:
> 
> -Downloaded Xcode 5 and installed command line tools, 
> -Upgraded to R 3.1 for Mavericks, and
> -Installed Rcpp from source. 
> 
> After installing Rcpp from source, I can write C++ functions using Inline. For example, 
> 
> library(Rcpp)
> library(inline)
> 
> cppFunction('
> double meanC(NumericVector x) {
>  int n = x.size();
>  double total = 0;
> 
>  for(int i = 0; i < n; ++i) {
>    total += x[i] / n;
>  }
>  return total;
> }'
>            )
> 
> works just fine, but the file containing 
> 
> #include <Rcpp.h>
> using namespace Rcpp;
> 
> // [[Rcpp::export]]
> double meanC(NumericVector x) {
>  int n = x.size();
>  double total = 0;
> 
>  for(int i = 0; i < n; ++i) {
>    total += x[i] / n;
>  }
>  return total;
> }
> 
> 
> will not compile. I get the error message:
> 
> $ R CMD SHLIB point_mcmc.cpp
> clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include    -fPIC  -Wall -mtune=core2 -g -O2  -c point_mcmc.cpp -o point_mcmc.o
> point_mcmc.cpp:1:10: fatal error: 'Rcpp.h' file not found
> #include <Rcpp.h>
>         ^
> 1 error generated.
> make: *** [point_mcmc.o] Error 1
> 

You cannot run vanilla R CMD SHLIB on Rcpp code since it requires flags to find Rcpp includes and libraries which are not part of R. From Rcpp docs:

— snip —

** Usage for package building

Rcpp provides a main header file Rcpp.h and a library inside the installed package in the directory lib. From within R, you can compute the directory location via system.file("lib", "Rcpp.h", package="Rcpp")--but both are provided for your use via the functions Rcpp::RcppCxxFlags() andRcpp::RcppLdFlags() functions. So we can just use the following as a file src/Makevars (or src/Makevars.win on Windows)
PKG_CXXFLAGS=`${R_HOME}/bin/Rscript -e "Rcpp:::CxxFlags()"`

PKG_LIBS=`${R_HOME}/bin/Rscript -e "Rcpp:::LdFlags()"`

See the help page for Rcpp-package for details.

— snip —

Cheers,
Simon


> The fact that one works and the other does not is incredibly confusing. Thanks in advance for any assistance. 
> 
> Best, 
> Josh 
> _______________________________________________
> R-SIG-Mac mailing list
> R-SIG-Mac at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-mac
> 



More information about the R-SIG-Mac mailing list