在 Windows 10 中将 GSL 库链接到 RcppGSL

Linking GSL libraries to RcppGSL in Windows 10

我编写了以下 .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.

你很接近了,因为你已经得到了 local323 文件。

还有两个步骤:

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

我测试过,你的代码有效。