I want to set -std=c++0x
, using Rcpp with inline.
I saw R: C++ Optimization flag when using the inline package but don't want to make a system-wide change, so I was trying option 2 in Dirk's answer.
I tried:
settings=getPlugin("Rcpp")
settings$Makevars[length(settings$Makevars)+1] = "CXXFLAGS = $(CXXFLAGS) -std=c++0x"
fun=cxxfunction(signature(x_ ="numeric"),src,plugin="Rcpp",settings=settings,verbose=2);
But the verbose output shows it is ignoring that. I also tried with CFLAGS, and without including existing value, but no effect.
(debug) Inserting the `g' flag tells the compiler to insert more information about the source code into the executable than it normally would. This makes use of a debugger such as gdb much easier, since it will be able to refer to variable names that occur in the source code.
Compiler flags are options you give to gcc when it compiles a file or set of files. You may provide these directly on the command line, or your development tools may generate them when they invoke gcc. This section describes just the flags that are specific to Objective-C.
Compile-time flags are boolean values provided through the compiler via a macro method. They allow to conditionally include or exclude code based on compile time conditions. There are several default flags provided by the compiler with information about compiler options and the target platform.
After some source code study, and a hint from Dirk Eddelbuettel, I've worked this out:
settings$env$PKG_CXXFLAGS='-std=c++0x'
You can set PKG_CPPFLAGS
the same way.
Here is a complete and more robust example:
library(inline)
src='
using namespace Rcpp;
std::vector<const char*> test={"Hello","World","!!!"};
return wrap(test);
'
settings=getPlugin("Rcpp")
settings$env$PKG_CXXFLAGS=paste('-std=c++0x',settings$env$PKG_CXXFLAGS,sep=' ')
fun=cxxfunction(signature(),src,plugin="Rcpp",settings=settings)
Sys.unsetenv('PKG_CXXFLAGS')
print(fun())
The paste()
makes sure if there were any settings already in the plugin then they are preserved.
The unsetenv()
is something cxxfunction
should already be doing (IMHO). Currently it will add variables to the environment, but not remove them after. So, without the unsetenv()
call, if you later ran cxxfunction
again, but with all defaults, any CXXFLAGS
you had earlier set would get used. This might not matter, or it might give surprising results. (Imagine if your were using PKG_CXXFLAGS
to set -Wall -Werror
for your own code, but later code links to a 3rd party library and refuses to compile with those options.)
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