如何更改和设置Rcpp编译参数 [英] How to change and set Rcpp compile arguments

查看:157
本文介绍了如何更改和设置Rcpp编译参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的 Rcpp 包(通过使用RStudio)。此程序包包含一个使用以下编译器选项编译的C ++函数:


clang ++ -I / Library / Frameworks / R.framework /资源/ 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


我想更改/设置这些参数,例如remove -g ,添加 -std = c ++ 11 并将参数 -O2 更改为 -O3

解决方案

工作关闭写R扩展,第1.2节,似乎你应该能够处理这个与几个shell脚本。作为一个最小的例子,(在Linux机器上工作),我从 Rcpp :: Rcpp.package.skeleton 创建了一个基本包,并将以下两个文件放在项目中根目录:



配置

  #!/ bin / bash 
如果[! -d〜/ .R];那么
mkdir〜/ .R; touch〜/ .R / Makevars
echoCXXFLAGS = -O3 -std = c ++ 11 -Wall -mtune = core2> 〜/ .R / Makevars
elif [! -e〜/ .R / Makevars];然后
touch〜/ .R / Makevars
echoCXXFLAGS = -O3 -std = c ++ 11 -Wall -mtune = core2> 〜/ .R / Makevars
else
mv〜/ .R / Makevars〜/ .R / Makevars.bak_CustomConfig
echoCXXFLAGS = -O3 -std = c ++ 11 -Wall - mtune = core2> 〜/ .R / Makevars
fi






清除



  .R / Makevars.bak_CustomConfig];然后
mv -f〜/ .R / Makevars.bak_CustomConfig〜/ .R / Makevars
fi


b $ b




然后使它们可执行( chmod 777 path / to / project / root / configure chmod 777 path / to / project / root / cleanup )。
当我运行Build和Reload时,我得到了(摘录):

  g ++ -m64 -I / usr / 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



这将覆盖R Makevars默认值,并使用正确的选项。






这只是一个基本的例子,所以你可能会想要进一步,取决于你的目标:


  1. 调整不同平台的脚本(例如Unix / Linux vs Windows / Windows 64位),这是我相信的链接文章。

  2. 确保从您的计算机设置文件的权限足以使这些文件在不同的计算机上执行(我认为它会工作,但我不是完全肯定) 。


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).

解决方案

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:

  1. 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.
  2. 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).

这篇关于如何更改和设置Rcpp编译参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆