问题
I created a new Rcpp
package (by using RStudio). This package contains a C++ function that is compiled by using the following compiler options:
clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I"/Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rcpp/include" -fPIC -Wall -mtune=core2 -g -O2 -c RcppExports.cpp -o RcppExports.o
I would like to change/set these arguments, for example remove -g
, add -std=c++11
and change the argument -O2
to -O3
. Also, it would be better to have the possibility to specify these changes once (for the package).
回答1:
Working off Writing R Extension, Section 1.2, it seems like you should be able to handle this with a couple of shell scripts. As a minimal example, (working on a Linux machine), I created a basic package from Rcpp::Rcpp.package.skeleton
, and put the following two files in the project root directory:
configure
#!/bin/bash
if [ ! -d "~/.R" ]; then
mkdir ~/.R; touch ~/.R/Makevars
echo "CXXFLAGS= -O3 -std=c++11 -Wall -mtune=core2" > ~/.R/Makevars
elif [ ! -e "~/.R/Makevars" ]; then
touch ~/.R/Makevars
echo "CXXFLAGS= -O3 -std=c++11 -Wall -mtune=core2" > ~/.R/Makevars
else
mv ~/.R/Makevars ~/.R/Makevars.bak_CustomConfig
echo "CXXFLAGS= -O3 -std=c++11 -Wall -mtune=core2" > ~/.R/Makevars
fi
cleanup
#!/bin/bash
if [ -e "~/.R/Makevars.bak_CustomConfig" ]; then
mv -f ~/.R/Makevars.bak_CustomConfig ~/.R/Makevars
fi
and then made them executable (chmod 777 path/to/project/root/configure
and chmod 777 path/to/project/root/cleanup
).
When I ran Build and Reload I got (excerpt):
g++ -m64 -I/usr/include/R -DNDEBUG
-I/usr/local/include
-I"/home/nr07/R/x86_64-redhat-linux-gnu-library/3.2/Rcpp/include"
-fpic -O3 -std=c++11 -Wall -mtune=core2
-c rcpp_hello.cpp -o rcpp_hello.o
g++ -m64 -shared -L/usr/lib64/R/lib
-Wl,-z,relro -o CustomConfig.so RcppExports.o rcpp_hello.o
-L/usr/lib64/R/lib -lR
which overrides the R Makevars defaults, and uses the correct options.
This was just a basic example, so you probably would want to take it a couple steps further, depending on your goals:
- Adapt the scripts for different platforms (e.g. Unix/Linux vs Windows/Windows 64-bit), which is touched on in the linked article I believe.
- Ensure that setting the permissions of the file from your machine is sufficient to for these files to be executed on a different computer (I think it will work, but I'm not completely positive).
回答2:
Thanks @nrussell and answered my question of 2 days searching! These solutions are an essential addition to the Rcpp
vignettes which have a high learning curve. To add to your instructions I did the build by:
- Being in an existing R project in RStudio, else do menu File>New project.
Build > Configure Build Tools > Choose 'package' and point to the package folder you've created previously i.e.
Rcpp.package.skeleton(name ="yourRpackageName",cpp_files = "yoursourcefile.cpp")
Then find the Build tab on the top-right pane and choose Install and Restart
- Run
sourceCpp
, e.g.sourceCpp(file ="anRpackage/src/rcpp_hello_world.cpp")
. To check it has compiled with the new instructions go to the bottom-right pane and click on the Source Cpp tab. - Then the example function
rcpp_hello_world()
will work.
来源:https://stackoverflow.com/questions/32586455/how-to-change-and-set-rcpp-compile-arguments