c++ - 来自 Rcpp 的 sourceCpp 在规范示例中给出了奇怪的编译错误

标签 c++ r rcpp

我收到以下奇怪的错误:

> sourceCpp( "comp.Cpp" )
Warning message:
In sourceCpp("comp.Cpp") :
No Rcpp::export attributes or RCPP_MODULE declarations found in source

当我使用 sourceCpp 时。 “comp.Cpp”文件如下所示:

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp:export]]
RcppExport SEXP comp(int n){
    int i;
    Rcpp::NumericVector product(n);
    for(i=0;i<n;i++){
        product[i]=i;
    }
    return(product);
}

我尝试将我的操作系统更新到 Maverick(然后不得不重新安装 Xcode 命令行工具和其他一些东西)但是这个错误早于那个。我可以制作测试包并安装它并运行它提供的 hello world,因此 Rcpp 包大部分都在工作。我还在 R 中运行时遇到另一个错误:

cppFunction( "
    int useCpp11() {
        auto x = 10;
        return x;
    }
", plugins=c("cpp11" ) )

这是

llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include     -I/usr/local/include  -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include"   -std=c++11  -fPIC  -mtune=core2  -O3    -c file69810a85a0d.cpp -o file69810a85a0d.o 
Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput,  : 
  Error 1 occurred building shared library.
cc1plus: error: unrecognized command line option "-std=c++11"
make: *** [file69810a85a0d.o] Error 1

不知道这两件事有没有关系。我认为我的编译器无法很好地处理属性,但在互联网上搜索并没有给我足够的教育来理解这一点。

如有任何帮助,我们将不胜感激。

最佳答案

将“[[Rcpp:export]]”更改为“[[Rcpp::export]]”。

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
SEXP comp(int n){
    int i;
    Rcpp::NumericVector product(n);
    for(i=0;i<n;i++){
        product[i]=i;
    }
    return(product);
}

关于c++ - 来自 Rcpp 的 sourceCpp 在规范示例中给出了奇怪的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21165591/

相关文章:

c++ - 将 char* 推送到 vector 时出现问题,但在每次迭代后都会将指向相同值缓冲区的指针添加到 vector 中

R:不要为 VennDiagram 写日志文件

R 代码不保存绘图图像

c++ - 如何访问 Rcpp 中的列表?

c++ - 我如何告诉 super 计算机上的 C++ 编译器我的 R 包需要 C++0x?

c++ - VC++ 模板编译器错误 C2244 : Unable to Match Function Definition to an Existing Declaration

c++ - 在 Boost::Program_Options 中,如何设置 wstring 的默认值?

c++ - 将运算符强制转换为模板参数的特化

r - 用平均值填补时间序列中的空白

c++ - 如何使用 RcppZiggurat 采样器为 Rcpp 函数设置 setseed?