Rh和Rmath.h在本机C程序中

“Rh”和“Rmath.h”是R.app和C之间接口的头文件。但是,它们似乎只能通过R命令’R CMD SHLIB something.c’读取。

我希望编译我的本机C程序,使用gcc包含它们。 我正在使用Snow Leopard,我无法找到那些头文件!

有帮助吗?

请参阅’Writing R Extensions’手册,了解详细信息,您可以轻松编译和链接Rmath.h和独立的R Math库 – 但不是Rh(您可以通过Rcpp / RInside使用,但这是一个不同的故事。)

有许多例子可供使用libRmath,一个在手册本身。 这是我在Debian软件包r-mathlib包含这个独立数学库的文件:

 /* copyright header omitted here for brevity */ #define MATHLIB_STANDALONE 1 #include  #include  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上你只需像这样构建(因为我将库和标题放在包中的标准位置;在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 $