“R.h”和“Rmath.h”是 R.app 和 C 之间接口(interface)的头文件。但是,它们似乎只能通过 R 命令“R CMD SHLIB something.c”读取
我希望使用 gcc 编译我的本地 C 程序以包含它们。我正在使用 Snow Leopard,但找不到这些头文件!
有什么帮助吗?
最佳答案
有关详细信息,请参阅“编写 R 扩展”手册,您可以轻松编译和链接 Rmath.h 和独立的 R 数学库——但不是 R.h。 (您可以通过 Rcpp/RInside 使用,但这是另一回事。)
有许多使用 libRmath 的示例,其中一个在手册中。这是我在 Debian 软件包 r-mathlib
中发布的一个,其中包含这个独立的数学库:
/* copyright header omitted here for brevity */
#define MATHLIB_STANDALONE 1
#include <Rmath.h>
#include <stdio.h>
typedef enum {
BUGGY_KINDERMAN_RAMAGE,
AHRENS_DIETER,
BOX_MULLER,
USER_NORM,
INVERSION,
KINDERMAN_RAMAGE
} N01type;
int
main(int argc, char** argv)
{
/* something to force the library to be included */
qnorm(0.7, 0.0, 1.0, 0, 0);
printf("*** loaded '%s'\n", argv[0]);
set_seed(123, 456);
N01_kind = AHRENS_DIETER;
printf("one normal %f\n", norm_rand());
set_seed(123, 456);
N01_kind = BOX_MULLER;
printf("normal via BM %f\n", norm_rand());
return 0;
}
在 Linux 上,您只需像这样构建(因为我将库和 header 放在包中的标准位置;在 OS X 上根据需要添加 -I 和 -L)
/tmp $ cp -vax /usr/share/doc/r-mathlib/examples/test.c mathlibtest.c
`/usr/share/doc/r-mathlib/examples/test.c' -> `mathlibtest.c'
/tmp $ gcc -o mathlibtest mathlibtest.c -lRmath -lm
/tmp $ ./mathlibtest
*** loaded '/tmp/mathlibtest'
one normal 1.119638
normal via BM -1.734578
/tmp $
关于c - native C 程序中的 R.h 和 Rmath.h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6961247/