c++ - Rcpp 代码示例 cppFunction

标签 c++ r rcpp

我正在尝试运行“与 Rcpp 无缝集成 R 和 C++”(第 32 页, list 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 

应该是code <-小写 c , 然后 #include <gsl/gsl_const_mksa.h>带有小写的“ell”。

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

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

这会告诉你

  1. object code not found从第一个错误开始,

  2. file....cpp:10:63: fatal error: gsL/gsl_const_mksa.h: No such file or directory

关于缺少的 header 。

但我现在确实看到,对于当前版本,我也得到了 __result not declared .将要 调查。

编辑:这是一个错误/更改。我写这一章的时候它起作用了,现在你需要

  1. 删除带有 #include <gsl/gsl_const_mksa.h> 的行来自 code作业

  2. 添加一个新的 includes=... cppFunction() 的参数调用如下:

更正的调用:

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

关于c++ - Rcpp 代码示例 cppFunction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19507333/

相关文章:

r - 数据框列的组合和排列

r - 从 Rcpp 函数打印整数向量

C++ 在窗口上输出文本

c++ - 一些哈希表/unordered_map 问题

c++ - 如何确保文件将在运行结束时关闭

r - 在 Shiny 的应用程序中使用 session 参数

r - 如何仅选择r中数据帧每年出现的个体

r - 将单个 Rcpp::IntegerVector 元素转换为字符

使用 Rcpp 重写 R 的 cummin() 函数并允许使用 NA

c++ - usleep 内部循环花费的时间太长