我正在尝试用 C 语言编写一个将由 R 调用的函数。在该函数中,我需要随机抽取一个样本,而无需从 vector 中进行替换。 Rmath.h 是否可以在 R 中使用类似 sample() 的东西?如果没有,有谁知道为什么我会得到 p>
Symbol not found: _gsl_rng_mt19937
当我尝试 dyn.load() 调用包含(带有适当的 header )的代码时:
#include <gsl/gsl_randist.h>
#include <gsl/gsl_rng.h>
void update_infs (int *inds,
int *inf_times,
int *n,
int *n_inf,
int *locs,
int *Rinds,
double *logmean,
double *logsd,
double *alpha,
double *wts,
int *indices /* a vector 1:n */
) {
...
/* set up GSL RNG */
gsl_rng * rng = gsl_rng_alloc(gsl_rng_mt19937);
/* end of GSL setup */
...
gsl_ran_choose(rng, tmp_inf_me, Rinds[i], indices, *n, sizeof (double));
... }
最佳答案
共享对象可能需要用像这样的命令编译
export PKG_CFLAGS=`gsl-config --cflags`
export PKG_LIBS=`gsl-config --libs`
然后
R CMD SHLIB gsl.c
或者替代地
PKG_CFLAGS=`gsl-config --cflags` PKG_LIBS=`gsl-config --libs` R CMD SHLIB gsl.c
这告诉编译器在哪里寻找 header ,以及链接器在哪里找到要链接的库。在编译和链接期间,命令应包含 gsl-config --cflags
和 gsl-config --libs
的输出,与
gcc -std=gnu99 -I/home/mtmorgan/bin/R-devel/include -I/usr/local/include -I/usr/include -fpi -c gsl.c -o gsl.o
在编译过程中
gcc -std=gnu99 -shared -L/usr/local/lib64 -o gsl.so gsl.o -L/usr/lib -lgsl -lgslcblas -lm -L/home/mtmorgan/bin/R-devel/lib -lR
链接期间。成功的考验是
R -e 'dyn.load("gsl.so")'
在一个包中,会有一个文件 src/Makevars
with
PKG_CFLAGS = `gsl-config --cflags`
PKG_LIBS = `gsl-config --libs`
并且,对于 gsl-config
可能不可用但用户已设法安装 gsl 并设置环境变量 LIB_GSL
的 Windows,文件 src/Makevars.win
PKG_LIBS += -L$(LIB_GSL)$(R_ARCH)/lib -lgsl -lgslcblas -lm
PKG_CPPFLAGS += -I$(RHOME)/src/include -I$(LIB_GSL)$(R_ARCH)/include
关于c - 在 C 中进行无替换采样以用于 R 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13369794/