我正在尝试构建一个需要可移植的R包(即能够安装在其他计算机上)。我正在使用一些C代码(使用.C()
调用)构建包,并使用gsl库。
当我尝试使用R CMD BUILD
构建程序包时,我收到一条错误消息,指出程序包或命名空间无法加载,因为它无法加载共享对象。下面我做了一个简单的玩具示例来重现我得到的错误。
myran.R文件:
myran <- function(){
a <- 1.1
nothing <- .C("myran",as.double(a))
return(nothing[[1]])
}
myran.C文件:
#include <stdio.h>
#include <time.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
void myran(double * a){
const gsl_rng * r = gsl_rng_alloc(gsl_rng_mt19937);
unsigned long int theseed;
theseed = time(NULL);
gsl_rng_set(r,theseed);
*a = gsl_ran_flat(r,0,1);
}
NAMESPACE文件:
exportPattern("^[[:alpha:]]+")
useDynLib(myran)
export(myran)
然后我运行R CMD BUILD myran
并获得以下输出:
* checking for file ‘myran/DESCRIPTION’ ... OK
* preparing ‘myran’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* installing the package to process help pages
-----------------------------------
* installing *source* package ‘myran’ ...
** libs
/usr/bin/cc -I/opt/local/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/opt/local/include -fPIC -pipe -Os -m64 -c myran.c -o myran.o
/usr/bin/cc -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/opt/local/Library/Frameworks/R.framework/Resources/lib -L/opt/local/lib -Wl,-headerpad_max_install_names -o myran.so myran.o -F/opt/local/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /private/var/folders/8y/f4_br32s1xn2d3rq2pswrtyr0000gw/T/RtmpIBvMbd/Rinst93381cca4b71/myran/libs
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
Error: package or namespace load failed for ‘myran’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/private/var/folders/8y/f4_br32s1xn2d3rq2pswrtyr0000gw/T/RtmpIBvMbd/Rinst93381cca4b71/myran/libs/myran.so':
dlopen(/private/var/folders/8y/f4_br32s1xn2d3rq2pswrtyr0000gw/T/RtmpIBvMbd/Rinst93381cca4b71/myran/libs/myran.so, 6): Symbol not found: _gsl_rng_mt19937
Referenced from: /private/var/folders/8y/f4_br32s1xn2d3rq2pswrtyr0000gw/T/RtmpIBvMbd/Rinst93381cca4b71/myran/libs/myran.so
Expected in: flat namespace
in /private/var/folders/8y/f4_br32s1xn2d3rq2pswrtyr0000gw/T/RtmpIBvMbd/Rinst93381cca4b71/myran/libs/myran.so
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/private/var/folders/8y/f4_br32s1xn2d3rq2pswrtyr0000gw/T/RtmpIBvMbd/Rinst93381cca4b71/myran’
-----------------------------------
ERROR: package installation failed
我需要更改哪些内容才能正确构建此程序包,使其可以移植并可以安装在其他计算机上?