其他分享
首页 > 其他分享> > c – Rcpp代码示例cppFunction

c – Rcpp代码示例cppFunction

作者:互联网

我正在尝试运行“使用Rcpp进行无缝R和C集成”的代码(第32页,代码清单2.10)但是它给出了一个错误.有人可以向我解释为什么不工作?谢谢

Code <- ' 
#include <gsL/gsl_const_mksa.h>           // decl of constants 
std::vector<double> volumes() { 
std::vector<double> v(5); 
v[0] = GSL_CONST_MKSA_US_GALLON;       // 1 US gallon 
v[1] = GSL_CONST_MKSA_CANADIAN_GALLON; // 1 Canadian gallon 
v[2] = GSL_CONST_MKSA_UK_GALLON;       // 1 UK gallon 
v[3] = GSL_CONST_MKSA_QUART;           // 1 quart 
v[4] = GSL_CONST_MKSA_PINT;            // 1 pint 
return v; 
}' 

gslVolumes <- cppFunction(code, depends="RcppGSL") 

这是消息错误:

file16e2b6cb966.cpp: In function ‘SEXPREC* sourceCpp_52966_volumes()’: 
file16e2b6cb966.cpp:30: error: ‘__result’ was not declared in this scope 
make: *** [file16e2b6cb966.o] Error 1 
llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include -I/usr/local/include  -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppGSL/include"    -fPIC  -mtune=core2 -g -O2  -c file16e2b6cb966.cpp -o file16e2b6cb966.o 
Erro em sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput,  : 
  Error 1 occurred building shared library. 

解决方法:

看起来你有错别字:

Code <- ' 
#include <gsL/gsl_const_mksa.h>           // decl of constants 

这应该是代码< - 用小写c,然后#include< gsl / gsl_const_mksa.h>用小写’ell’.

一般来说,我建议打开详细模式:

gslVolumes <- cppFunction(code, depends="RcppGSL", verbose=TRUE) 

本来会告诉你的

>从第一个错误中找不到对象代码,和
> file …. cpp:10:63:致命错误:gsL / gsl_const_mksa.h:没有这样的文件或目录

关于丢失的标题.

但我现在确实看到,对于当前版本,我也没有声明__result.将
调查.

编辑:这是一个错误/变化.它在我写这一章时起作用,现在你需要

>使用#include< gsl / gsl_const_mksa.h>删除该行从代码分配
>在cppFunction()调用中添加一个新的includes = …参数,如下所示:

更正的电话:

 gslVolumes <- cppFunction(code, depends="RcppGSL",
                           includes="#include <gsl/gsl_const_mksa.h>")

标签:c,r,rcpp
来源: https://codeday.me/bug/20190728/1565198.html