在 Windows 机器上将 GSL 库链接到 RcppGSL [英] Linking GSL library to RcppGSL on Windows machine
问题描述
我尝试将 GSL 库链接到 RcppGSL 包.以下是我的测试函数:
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
}
如果以下命令没有抛出编译器错误,则 GSL 库已成功链接到 RcppGSL:
The GSL library is succesfully linked to the RcppGSL if the following command does not throw a compiler error:
sourceCpp("colNorm.cpp")
由于我使用的是 Windows 机器,所以我需要定义环境变量,以便 RcppGSL 知道 GSL 库所在的位置.
Since I am using Windows machine, I need to define environment variables such that RcppGSL knows where the GSL library is located.
我尝试编辑环境变量,但以下编译错误表明该包仍然无法找到 GSL 库:
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
我设置了一个环境变量 LIB_GSL
等于 "C:/CodeLibrary/lib"
.在第一行,编译器获取我的环境变量并添加 /include
.在第二行中,编译器添加了 /lib
.我的C盘上不存在这些位置,也许这就是它找不到库的原因.
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.
如果有很多编译器经验的人可以展示如何成功地将第 3 方库链接到 Windows 机器上的包,我会非常高兴.
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?
推荐答案
感谢 Dirk 的支持!我终于启动并运行了.
Thanks Dirk for your handholding! I got it finally up and running.
必须做的三件事:
1) 下载 local300 文件夹从您的链接和分配驱动器上的文件夹.路径不能包含任何空格,即 C:/Program Files/local300
将不起作用但C:/local300
会工作
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) 设置环境变量 LIB_GSL
等于这个路径,例如LIB_GSL
到 C:/local300
2) Set the environment variable LIB_GSL
equal to this path, e.g. LIB_GSL
to C:/local300
3) 编译器查看 LIB_GSL/lib
的 libgsl.a
和 libgslcblas.a
(-lgsl 和 -lgslcblas).但是,在 LIB_GSL/lib
中有子文件夹 i386
和 x64
.我不知道如何更改编译器查找文件的位置,因此我从 x64
内部复制了所有内容并将其放入 LIB_GSL/lib
文件夹(一个文件夹级别以上).
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).
这允许 RcppGSL 编译代码而不会出错.
This allows RcppGSL to compile code without errors.
这篇关于在 Windows 机器上将 GSL 库链接到 RcppGSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!