我想为以下 C 函数生成 R(编程语言)包装器:
int *test(void)
{
int i;
i = 1024;
return (int *) i;
}
为了创建这个包装器,我使用 SWIG 和以下接口(interface):
%module X
%{
extern int *test(void);
%}
extern int *test(void);
我可以成功创建并编译这个包装器。但是,当我运行以下 R 代码时(包装器已预先加载):
print(test())
会出现如下错误:
Error in getClass(Class, where = topenv(parent.frame())) :
“_p_int” is not a defined class
Calls: print -> test -> new -> getClass
我的问题如下:如何包装 C test
函数(更准确地说是该函数返回的 int *
)?
最佳答案
SWIG 在 R 周围的使用并不广泛。但是 inline包可以帮助你。
R> library(inline)
R> foo <- cfunction(signature(i="int"), body="*i = 1024;",
+ language="C", convention=".C")
R> foo(12)$i
[1] 1024
R>
也就是说,您可能需要 .Call()
而不是 .C()
,并且您可能应该查看 Rcpp即使您想使用纯 C,该工具也非常丰富且有用。
关于c - 如何为返回 int* 的 C 函数生成 R 包装器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45869425/