如何使用Boost库在C ++与Rcpp [英] How to use Boost library in C++ with Rcpp

查看:248
本文介绍了如何使用Boost库在C ++与Rcpp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 3.0.0 上使用 Rcpp 包。我想尝试 此示例 ,但我不能,因为我不知道如何使用 Boost



我在目录/ Users / giorgi / boost_1_53_0中安装了Boost,因此我设置了 Sys.setenv(PKG_CXXFLAGS= - I / Users / giorgi / boost_1_53_0)但我不知道我在做正确的事情。对不起,我对这个东西很无知!

解决方案

我会尝试几件事:


  1. 使用Boost编写一个三行独立的C ++程序,并进行编译。这只是为了证明你自己拥有 -I / some / dir 标志。


  2. 编写一个简单的Rcpp函数并使用 sourceCpp()编译和加载它。


  3. 文件〜/ .R / Makevars ,并将 -I 标志从1.这里设置为 CXXFLAGS CFLAGS 这两个将被 R CMD ... ,因此 sourceCpp()


  4. 如果一切都失败,添加 LinkingTo:BH 作为CRAN包BH提供了可以使用的Boost头(一旦你安装BH)。


编辑,大约1 1/2年后



您也可以在此代码中使用 // [[Rcpp :: depends(BH)]]
$ b

  #include< Rcpp.h> 
#include< boost / math / common_factor.hpp> //包含在BH

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

使用命名空间Rcpp;

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

它在同时更新Rcpp和BH的同时构建和运行正常:

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


I am using Rcpp package on R 3.0.0. I am trying to run this example, but I cannot because I don't know how to use 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. 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. This is just to prove to yourself that you have the -I/some/dir flag right.

  2. Write a simple Rcpp function and use eg sourceCpp() to compile and load it.

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

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

Edit, about 1 1/2 years later

You can also use a // [[Rcpp::depends(BH)]] as eg in this code

#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:

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

这篇关于如何使用Boost库在C ++与Rcpp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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