【问题标题】:Rcpp code example cppFunctionrcpp 代码示例 cppFunction
【发布时间】:2013-10-22 16:27:28
【问题描述】:

我正在尝试运行“无缝 R 和 C++ 与 Rcpp 集成”的代码(第 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. 

【问题讨论】:

    标签: c++ r rcpp


    【解决方案1】:

    你好像有错别字:

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

    应该是code &lt;- 和小写c,然后是#include &lt;gsl/gsl_const_mksa.h&gt; 和小写“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

    关于缺少的标题。

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

    编辑:这是一个错误/更改。我写章节的时候就成功了,现在你需要

    1. code 分配中删除带有#include &lt;gsl/gsl_const_mksa.h&gt; 的行

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

    正确的调用:

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

    【讨论】:

    • 本书有勘误/更新页面吗?在找到这个 SO 页面之前,我查看了其他几个地方。
    • 不,我有点落后于创建一个。但是,如果你给我发电子邮件,条目更改会变得更好,我就开始了。不幸的是,在接下来的几周内不会。至于关于 Rcpp 的“帮助”,文档已经并将指向 rcpp-devel 列表。
    【解决方案2】:

    除了 Dirk 所说的,我建议您将代码提升为 .cpp 文件。

    // [[Rcpp::depends(RcppGSL)]]
    #include <Rcpp.h>
    #include <gsl/gsl_const_mksa.h>           // decl of constants 
    
    // [[Rcpp::export]]
    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; 
    }   
    

    然后,您可以sourceCpp 该文件。

    【讨论】:

    • 非常有帮助;我尝试了 similar 失败,但这有效。请注意,通过在R 中发出volumes()(而不是原始示例中的gslVolumes())来调用由sourceCpp 处理此文件产生的函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 2011-12-17
    • 2016-12-01
    相关资源
    最近更新 更多