1

I try to link the GSL library to the RcppGSL package. The following is my test function:

# colNorm.cpp
// [[Rcpp::depends(RcppGSL)]]
#include <RcppGSL.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>

extern "C" SEXP colNorm(SEXP sM) {
    try {   
        RcppGSL::matrix<double> M = sM; // create gsl data structures from SEXP
        int k = M.ncol();
        Rcpp::NumericVector n(k); // to store results 

        for (int j = 0; j < k; j++) {
            RcppGSL::vector_view<double> colview = gsl_matrix_column (M, j);
            n[j] = gsl_blas_dnrm2(colview);
        }
        M.free() ;
        return n; // return vector  

    } catch( std::exception &ex ) {
    forward_exception_to_r( ex );

    } catch(...) { 
        ::Rf_error( "c++ exception (unknown reason)" ); 
    }
    return R_NilValue; // -Wall
}

The GSL library is succesfully linked to the RcppGSL if the following command does not throw a compiler error:

sourceCpp("colNorm.cpp")

Since I am using Windows machine, I need to define environment variables such that RcppGSL knows where the GSL library is located.

I tried editing the environment variable, but the following compiler error shows that the package is still unable to find the GSL library:

g++ -m64 -I"C:/PROGRA~1/R/R-31~1.1/include" -DNDEBUG -I"C:/CodeLibrary/lib"/include     -               I"C:/PROGRA~1/R/R-31~1.1/library/Rcpp/include" -I"C:/PROGRA~1/R/R-31~1.1/library/RcppGSL/include"  -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -mtune=core2 -c colNorm.cpp -o colNorm.o
g++ -m64 -shared -s -static-libgcc -o sourceCpp_38624.dll tmp.def colNorm.o -LC:/CodeLibrary/lib/lib -lgsl -lgslcblas -LC:/PROGRA~1/R/R-31~1.1/bin/x64 -lRlapack -LC:/PROGRA~1/R/R-31~1.1/bin/x64 -lRblas -lgfortran -Ld:/RCompile/CRANpkg/extralibs64/local/lib/x64 -Ld:/RCompile/CRANpkg/extralibs64/local/lib -LC:/PROGRA~1/R/R-31~1.1/bin/x64 -lR
c:/program files/r/r-3.1.1/rtools/gcc-4.6.3/bin/../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -lgsl
c:/program files/r/r-3.1.1/rtools/gcc-4.6.3/bin/../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -lgslcblas
collect2: ld returned 1 exit status

I set an environment variable LIB_GSL equal to "C:/CodeLibrary/lib". In the first line, the compiler takes my environment variable and adds /include. In the second line, the compiler adds /lib. Those locations do not exist on my C drive, maybe that's the reason why it cannot find the library.

I would be really happy, if someone with a lot of compiler experience could show how to successfully link a 3rd party library to a package on Windows machine.

Maybe more environment variables need to be defined?

Fka
  • 6,044
  • 5
  • 42
  • 60

2 Answers2

3

Thanks Dirk for your handholding! I got it finally up and running.

Three things have to be done:

1) Download the local300 folder from your link and allocate folder on your drive. The path cannot contain any whitespaces, i.e. C:/Program Files/local300 will not work but C:/local300 will work

2) Set the environment variable LIB_GSL equal to this path, e.g. LIB_GSL to C:/local300

3) The compiler looks at LIB_GSL/lib for libgsl.a and libgslcblas.a (-lgsl and -lgslcblas). However, in LIB_GSL/lib are subfolders i386 and x64. I didn't know how to change the place the compiler looks for the files, thus I copied everything from inside x64 and put it into LIB_GSL/lib folder (one folder level above).

This allows RcppGSL to compile code without errors.

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • 2
    Thanks for taking the time to archive these pointers! `local300` is now `local320`, but with that one change, these instructions finally got me a functional **RcppGSL** on my Windows box. – Josh O'Brien Sep 07 '15 at 02:14
2

Look at this page which is (after a link or two) pointed to from the R Installation and Administration manual, appendix D for Windows.

Expand that, see where it puts headers and the library for GSL and adjust LIB_GSL accordingly.

This is what CRAN itself uses, so we know it works.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725