我正在尝试运行“与 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)
这会告诉你
object code not found
从第一个错误开始,file....cpp:10:63: fatal error: gsL/gsl_const_mksa.h: No such file or directory
关于缺少的 header 。
但我现在确实看到,对于当前版本,我也得到了 __result not declared
.将要
调查。
编辑:这是一个错误/更改。我写这一章的时候它起作用了,现在你需要
删除带有
#include <gsl/gsl_const_mksa.h>
的行来自code
作业添加一个新的
includes=...
cppFunction()
的参数调用如下:
更正的调用:
gslVolumes <- cppFunction(code, depends="RcppGSL",
includes="#include <gsl/gsl_const_mksa.h>")
关于c++ - Rcpp 代码示例 cppFunction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19507333/