如何制作内联 C++ R 中的函数调用?
好吧,我正在用 R 编程,我想创建一个 C++ 函数。我已经导入了 Rcpp 和内联库。现在,我只是想创建一个简单的函数来添加 2 个数字,但无论我尝试什么,我都会遇到错误。
这是我的代码:
cppstring = 'double ss = RcppSexp(s).asDouble(); return RcppSexp(ss+4).asSexp();'
hi <- cfunction(body=cppstring, signature(s="double"), Rcpp = TRUE)
当我输入第二行时,我发现
file628a34ce.cpp: In function ‘SEXPREC* file628a34ce(SEXPREC*)’:
file628a34ce.cpp:9: error: ‘RcppSexp’ was not declared in this scope
make: *** [file628a34ce.o] Error 1
ERROR(s) during compilation: source code errors or compiler configuration errors!
Program source:
1: #include <Rcpp.h>
2:
3:
4: extern "C" {
5: SEXP file628a34ce ( SEXP s );
6: }
7:
8: SEXP file628a34ce ( SEXP s ) {
9: double ss = RcppSexp(s).asDouble(); return RcppSexp(ss+4).asSexp();
10: Rf_warning("your C program does not return anything!");
11: return R_NilValue;
12: }
Error in compileCode(f, code, language, verbose) :
Compilation ERROR, function(s)/method(s) not created! file628a34ce.cpp: In function ‘SEXPREC* file628a34ce(SEXPREC*)’:
file628a34ce.cpp:9: error: ‘RcppSexp’ was not declared in this scope
make: *** [file628a34ce.o] Error 1
我已经尝试了我能想到的一切,从强制转换,到移动代码,到#include RcppSexp,到简单的返回,每次我得到一些错误,无论是
cannot convert ‘double’ to ‘SEXPREC*’ in return
还是
invalid use of undefined type ‘struct SEXPREC’
什么
forward declaration of ‘struct SEXPREC’
...我很困惑:(我在网上看了几个例子,我目前所拥有的似乎是其他人正在做的事情,而且它对他们来说神奇地有效...
是我一直看到这个 SEXPREC* 的东西它所做的 extern "C" 函数是什么?为什么它在我的 return 语句之后生成语句并告诉我我的函数没有返回任何内容,即使它返回了?
All right, so I'm programming in R and I want to make a C++ function. I've imported the Rcpp and inline libraries. For right now, I'm just trying to make a simple function that adds 2 numbers, but no matter what I try, I get errors.
Here's my code:
cppstring = 'double ss = RcppSexp(s).asDouble(); return RcppSexp(ss+4).asSexp();'
hi <- cfunction(body=cppstring, signature(s="double"), Rcpp = TRUE)
and when I enter that second line, I get
file628a34ce.cpp: In function ‘SEXPREC* file628a34ce(SEXPREC*)’:
file628a34ce.cpp:9: error: ‘RcppSexp’ was not declared in this scope
make: *** [file628a34ce.o] Error 1
ERROR(s) during compilation: source code errors or compiler configuration errors!
Program source:
1: #include <Rcpp.h>
2:
3:
4: extern "C" {
5: SEXP file628a34ce ( SEXP s );
6: }
7:
8: SEXP file628a34ce ( SEXP s ) {
9: double ss = RcppSexp(s).asDouble(); return RcppSexp(ss+4).asSexp();
10: Rf_warning("your C program does not return anything!");
11: return R_NilValue;
12: }
Error in compileCode(f, code, language, verbose) :
Compilation ERROR, function(s)/method(s) not created! file628a34ce.cpp: In function ‘SEXPREC* file628a34ce(SEXPREC*)’:
file628a34ce.cpp:9: error: ‘RcppSexp’ was not declared in this scope
make: *** [file628a34ce.o] Error 1
I've tried everything I could possibly think of, from casting, to moving code around, to #including RcppSexp, to just plain returning s, and every time I get some error, whether it's
cannot convert ‘double’ to ‘SEXPREC*’ in return
or
invalid use of undefined type ‘struct SEXPREC’
or
forward declaration of ‘struct SEXPREC’
...I'm so confused :( I've looked at several examples online, and what I currently have seems to be what everyone else is doing, and it magically works for them...
What is this SEXPREC* thing I keep seeing everywhere? And what is that extern "C" function thing that it makes? And why does it generate statements AFTER my return statement and tell me that my function doesn't return anything, even though it does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否有理由不从(字面意思!!)数十个使用内联的 Rcpp 示例开始?
另外,RcppSexp 到底是什么?您关注哪些文档?
这是我昨晚为 rcpp-devel 上的某人所做的一个示例(您可能应该加入):
这是一个更高级的示例,因为它使用 Rcpp Sugar 为我们提供矢量化表达式 a C++ 中的 R,我们在这里使用 Rcpp Sugar 中的简单
sapply()
进行演示:这最清楚地演示了您的两个请求:我们使用隐式模板转换器
as<>()
从 R 给出的SEXP
转换为初始向量,然后使用隐式模板转换器wrap()
返回转换后的第二个向量。所有这些都在 Rcpp-introduction vignette 和 Rcpp 文档中的其他 vignettes 中进行了详细解释。
Is there a reason you are not starting from the (literally!!) dozens of Rcpp examples using inline?
Also, what on earth is RcppSexp? What documentation are your following?
Here is an example I did last night for someone on the rcpp-devel (which you should probably join):
This is a more advanced example as it uses Rcpp sugar to give us vectorised expression a la R in C++, which we demonstrate here with a the simple
sapply()
from Rcpp sugar:This most clearly demonstrates two of your requests: we use the implicit template converters
as<>()
to go from aSEXP
given from R to the initial vector, and then use the implicit template converterwrap()
to return the transformed second vector.All this is explained in detail in the Rcpp-introduction vignette and the other vignettes in the Rcpp documentation,.