I have some strange troubles with Rcpp - it uses unpredictable C++ compiler. This question is somewhat similar to this question.
I'm on OSX, I have 2 complilers - default clang
and clang-omp
with openmp support. Also I have following ~/.R/Makevars
file (where I set up clang-omp
as default compiler):
CC=clang-omp
CXX=clang-omp++
CFLAGS += -O3 -Wall -pipe -pedantic -std=gnu99
CXXFLAGS += -O3 -Wall -pipe -Wno-unused -pedantic -fopenmp
The problem is that, the package I'm developing compiles with clang++
, not clang-omp++
. I also tried (as experiment to lacate issue) to change package src/Makevars
and set CXX=clang-omp++
and moreover modified $R_HOME/etc/Makeconf
CXX
entry to CXX = clang-omp++
. No luck - it still compiles with clang++
. Have no idea why it happens.
Also here is small reproducible (both from console R and from Rstudio) example (don't know whether it related to issue above).
Suppose 2 very similar cpp functions:
1.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
return x * 2;
}
Call sourceCpp
from R:
library(Rcpp)
sourceCpp("src/Rcpp_compiler.cpp", verbose = T)
/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB -o 'sourceCpp_1.so' 'Rcpp_compiler.cpp'
clang-omp++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I"/Users/dmitryselivanov/Library/R/3.2/library/Rcpp/include" -I"/Users/dmitryselivanov/projects/experiments/src" -fPIC -Wall -mtune=core2 -g -O2 -O3 -Wall -pipe -Wno-unused -pedantic -fopenmp -c Rcpp_compiler.cpp -o Rcpp_compiler.o
clang-omp++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o sourceCpp_1.so Rcpp_compiler.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
Work as expected - uses clang-omp++ and all my flags from ~/.R/Makevars
2.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
return x * 2;
}
Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB -o 'sourceCpp_2.so' 'Rcpp_compiler.cpp'
clang++ -std=c++11 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I"/Users/dmitryselivanov/Library/R/3.2/library/Rcpp/include" -I"/Users/dmitryselivanov/projects/experiments/src" -fPIC -Wall -mtune=core2 -g -O2 -c Rcpp_compiler.cpp -o Rcpp_compiler.o
clang++ -std=c++11 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o sourceCpp_2.so Rcpp_compiler.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
I only added // [[Rcpp::plugins(cpp11)]]
and it compiles with clang++
instead of clang-omp++
Here is my sessionInfo()
:
R version 3.2.1 (2015-06-18) Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)
locale: 1 en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages:
1 stats graphics grDevices utils datasets methods base
other attached packages: 1 Rcpp_0.12.1
loaded via a namespace (and not attached): 1 tools_3.2.1
Default compiler behavior By default, the compiler determines the source language by examining the source filename extension. For example, filename. c indicates C, while filename. cpp indicates C++03, although the command-line options --c90 , --c99 , --cpp , and --cpp11 let you override this.
Description The 'Rcpp' package provides R functions as well as C++ classes which offer a seamless integration of R and C++. Many R data types and objects can be mapped back and forth to C++ equivalents which facilitates both writing of new code as well as easier integration of third-party libraries.
Compilers are utility programs that take your code and transform it into executable machine code files. When you run a compiler on your code, first, the preprocessor reads the source code (the C++ file you just wrote). The preprocessor searches for any preprocessor directives (lines of code starting with a #).
We can use the function sourceCpp to read a function written in C++ into R interactively. The function takes care of the compilation using R CMD SHLIB and automatically generates an R wrapper for the underlying function.
Thanks, to @Dirk hint, I finaly got an answer. Hope, this will save a little bit of time for somebody.
Following two lines in ~/.R/Makevars
solved my problem:
CXX1X=clang-omp++
See details in this Writing R Extensions section.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With