c - 如何为返回 int* 的 C 函数生成 R 包装器?

标签 c r swig

我想为以下 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/

相关文章:

c - 痛饮 ruby "TypeError: Expected argument 0 of type int *, but got Fixnum 0"

c - glClearColor(0, 0, 0, 0) 是透明的,不是黑色

python - 编译时如何捕获更改的 .c 文件列表?

C指针字符数组到字符数组

r - 如何在 Shiny R 文件上传示例中使用 renderDataTable

windows - 从 C++ 源文件创建 Windows DLL

c - protobuf c 字符串解码

R格子: How to adjust the legend marker line width?

复制交替向量值

c - 编译 SWIG 的包装器模块输出时出现 6 个错误?