noirritchandra
noirritchandra

Reputation: 115

Linking GSL libraries to RcppGSL in Windows 10

I have written the following .cpp file to draw samples from Dirichlet distribution using the random number distribution function in GSL. The filename is C_functions.cpp. I am doing everything in 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);
}

I have created an environmental variable LIB_GSL and set its value as "C:/Users/nkc10/Documents/R/local323". This is the location where I unzipped the local323.zip folder downloaded from this link .

However, when I compile it with sourceCpp the following error is shown

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

Upvotes: 2

Views: 1215

Answers (1)

inferator
inferator

Reputation: 494

You are close, since you already got the local323 file.

Two more steps:

  1. Copy the c:\local323\include\gsl folder into your RcppGSL folder (i.e., C:\Users\YOU\Documents\R\win-library\3.6\RcppGSL\include) OR into your project directory. I tried the latter, but I think it should work either way.
  2. From the local323 package, copy libgsl.a libgslcblas.a to c:\Rtoools\mingw_64\libs. I have no idea why it doesn't find it in your c:\local323 folder, but this will work.

And I tested it, your code works.

Upvotes: 4

Related Questions