简体   繁体   English

如何在C ++中使用Rcpp的Boost库

[英]How to use Boost library in C++ with Rcpp

I am using Rcpp package on R 3.0.0 . 我在R 3.0.0上使用Rcpp包。 I am trying to run this example , but I cannot because I don't know how to use Boost . 我试图运行这个例子 ,但我不能,因为我不知道如何使用Boost

I installed Boost in the directory /Users/giorgi/boost_1_53_0 therefore I set Sys.setenv("PKG_CXXFLAGS"="-I /Users/giorgi/boost_1_53_0") but I am not sure I am doing the right thing. 我在目录/ Users / giorgi / boost_1_53_0中安装了Boost,因此我设置了Sys.setenv("PKG_CXXFLAGS"="-I /Users/giorgi/boost_1_53_0")但我不确定我做的是正确的。 Sorry but I am quite ignorant with this stuff! 对不起,但我对这些东西一无所知!

I would try a few things: 我会尝试一些事情:

  1. Write a three line standalone C++ program using Boost, and compile it. 使用Boost编写一个三行独立的C ++程序,并进行编译。 This is just to prove to yourself that you have the -I/some/dir flag right. 这只是为了向自己证明你有-I/some/dir标志。

  2. Write a simple Rcpp function and use eg sourceCpp() to compile and load it. 编写一个简单的Rcpp函数并使用例如sourceCpp()来编译和加载它。

  3. Create a file ~/.R/Makevars and set the -I flag from 1. here as either one one of CXXFLAGS or CFLAGS both of which will be used by R CMD ... and hence sourceCpp() . 创建一个文件~/.R/Makevars并在此处设置-I标志,作为CXXFLAGSCFLAGS一个,这两个将由R CMD ...以及sourceCpp()

  4. If everything else fails, create a small package and add LinkingTo: BH as the CRAN package BH provides Boost headers you can use (once you install BH). 如果其他一切都失败了,创建一个小包并添加LinkingTo: BH作为CRAN包BH提供你可以使用的Boost头(一旦你安装BH)。

Edit, about 1 1/2 years later 编辑,大约1年半之后

You can also use a // [[Rcpp::depends(BH)]] as eg in this code 您也可以在此代码中使用// [[Rcpp::depends(BH)]]

#include <Rcpp.h>
#include <boost/math/common_factor.hpp>  // included in BH  

// [[Rcpp::depends(BH)]]    

using namespace Rcpp;

// [[Rcpp::export]]   
int computeGCD(int a, int b) {
  return boost::math::gcd(a, b);
}

which builds and runs just fine as we updated both Rcpp and BH in the meantime: 在此期间我们更新Rcpp和BH时构建和运行得很好:

R> library(Rcpp)
R> sourceCpp("/tmp/simpleBoost.cpp")
R> computeGCD(6, 15)
[1] 3
R> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM