/* -*- author: Tan Menglong; email: tanmenglong_at_gmail; twitter/weibo: @crackcell; 转载请注明出处 -*- */
#include <R.h>
void hello(const char **msg) {
Rprintf("msg from R: %s", *msg);
}
R CMD SHLIB hello.c
> dyn.load('hello.so')
> .C("hello", as.character("hello world"))
msg from R: hello world[[1]]
[1] "hello world"
这个例子是带返回值的。
#include <R.h>
void plus(const int *a, const int *b, int *result) {
*result = *a + *b;
}
> dyn.load('hello.so')
> .C("plus", as.integer(1), as.integer(2), as.integer(0))[[3]]
[1] 3
注意,这里传参的时候,第3个返回值的参数也需要传。.C函数返回一个列表,列表的每个项保存参数的值。这里,使用的是第3个,也就是result。
Date: 2012-04-04 16:29:34 CST
HTML generated by org-mode 6.33x in emacs 23
Modified theme and code from Tom Preston-Werner. Hosted by Baidu App Engine.