在Windows 10中将GSL库链接到RcppGSL

时间:2019-05-03 20:14:22

标签: c++ r windows-10 rcpp gsl

我编写了以下.cpp文件,使用GSL中的随机数分布函数从Dirichlet分布中提取样本。文件名是C_functions.cpp。我正在Windows 10中做所有事情。

#include <RcppArmadillo.h>
#include <RcppGSL.h>
// #include <Rcpp.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::depends(RcppGSL)]]



using namespace arma;


// [[Rcpp::export]]
vec rdirichlet_arma(vec a){
  const gsl_rng_type * T;
  gsl_rng * r;

    /* create a generator chosen by the
   environment variable GSL_RNG_TYPE */

  gsl_rng_env_setup();

  T = gsl_rng_default;
  r = gsl_rng_alloc (T);

  /* print n random variates chosen from
   the poisson distribution with mean
   parameter mu */

  int n=a.size();
  vec results(n);

  gsl_ran_dirichlet(r, n, a.begin(), results.begin());

  gsl_rng_free (r);
  a.reset();
  return results;
}

// [[Rcpp::export]]
double pow2(double x){
  return gsl_pow_2(x);
}

我创建了一个环境变量LIB_GSL并将其值设置为“ C:/ Users / nkc10 / Documents / R / local323”。这是我解压缩从this link 下载的local323.zip文件夹的位置。

但是,当我使用sourceCpp进行编译时,会显示以下错误

>C:/RBuildTools/3.5/mingw_64/bin/g++  -std=gnu++11 -I"C:/PROGRA~1/R/R-35~1.2/include" -DNDEBUG -I../inst/include -fopenmp -I/include  -I"C:/Users/nkc10/Documents/R/win-library/3.5/Rcpp/include" -I"C:/Users/nkc10/Documents/R/win-library/3.5/RcppArmadillo/include" -I"C:/Users/nkc10/Documents/R/win-library/3.5/RcppGSL/include" -I"C:/Users/nkc10/Dropbox/Research/sparse_bayesian_infinite_factor_models-master"        -O2 -Wall  -mtune=generic -c C_functions.cpp -o C_functions.o
In file included from C:/Users/nkc10/Documents/R/win-library/3.5/RcppGSL/include/RcppGSL.h:25:0,
                 from C_functions.cpp:2:
C:/Users/nkc10/Documents/R/win-library/3.5/RcppGSL/include/RcppGSLForward.h:26:29: fatal error: gsl/gsl_vector.h: No such file or directory
 #include <gsl/gsl_vector.h> 
                             ^
compilation terminated.
make: *** [C:/PROGRA~1/R/R-35~1.2/etc/x64/Makeconf:215: C_functions.o] Error 1
Error in sourceCpp("C_functions.cpp") : 
  Error 1 occurred building shared library.

1 个答案:

答案 0 :(得分:1)

您已经关闭,因为您已经获得了local323文件。

再执行两个步骤:

  1. c:\local323\include\gsl文件夹复制到RcppGSL文件夹(即C:\Users\YOU\Documents\R\win-library\3.6\RcppGSL\include)或复制到项目目录。我尝试了后者,但我认为无论哪种方式都可以。
  2. 从local323程序包中,将libgsl.a libgslcblas.a复制到c:\Rtoools\mingw_64\libs。我不知道为什么在您的c:\ local323文件夹中找不到它,但这会起作用。

我对其进行了测试,您的代码有效。