使用Rcpp和typedef进行编译时出错 [英] Compilation error using Rcpp with typedef

查看:97
本文介绍了使用Rcpp和typedef进行编译时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RcppEigen为我的R包编写一些C ++组件,在这种情况下使用typedef遇到了麻烦.以下代码无法编译:

I'm writing some C++ components for my R package using RcppEigen and I'm having trouble using typedefs in this context. The following code won't compile:

// [[Rcpp::depends(RcppEigen)]]
#include <RcppEigen.h>

using namespace Rcpp;

typedef Eigen::ArrayXd MapAr1;

// [[Rcpp::export]]
MapAr1 myFun(const MapAr1& x){

  MapAr1 y = x;
  y[0] = 0;

  return y;

}

这是错误:

==> Rcpp::compileAttributes()

* Updated src/RcppExports.cpp
* Updated R/RcppExports.R

==> devtools::document(roclets=c('rd', 'collate', 'namespace'))

Updating my_package documentation
Loading my_package
Re-compiling my_package
'/usr/lib64/R/bin/R' --no-site-file --no-environ --no-save --no-restore  \
  --quiet CMD INSTALL '/my_path/my_package'  \
  --library='/tmp/RtmpgPjAdf/devtools_install_125071da0b53' --no-R --no-data  \
  --no-help --no-demo --no-inst --no-docs --no-exec --no-multiarch  \
  --no-test-load 

* installing *source* package ‘my_package’ ...
g++ -m64 -I/usr/include/R -DNDEBUG  -I/usr/local/include -I"/my_path/R/x86_64-redhat-linux-gnu-library/3.3/Rcpp/include" -I" /my_path/R/x86_64-redhat-linux-gnu-library/3.3/RcppEigen/include"   -fpic  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches   -m64 -mtune=generic  -c RcppExports.cpp -o RcppExports.o
** libs
RcppExports.cpp:10:1: error: ‘MapAr1’ does not name a type
 MapAr1 myFun(const MapAr1& x);
 ^
RcppExports.cpp: In function ‘SEXPREC* my_package_myFun(SEXP)’:
RcppExports.cpp:15:42: error: ISO C++ forbids declaration of ‘type name’ with no type [-fpermissive]
     Rcpp::traits::input_parameter< const MapAr1& >::type x(xSEXP);
                                          ^
RcppExports.cpp:15:50: error: template argument 1 is invalid
     Rcpp::traits::input_parameter< const MapAr1& >::type x(xSEXP);
                                                  ^
RcppExports.cpp:15:58: error: expected initializer before ‘x’
     Rcpp::traits::input_parameter< const MapAr1& >::type x(xSEXP);
                                                          ^
RcppExports.cpp:16:40: error: ‘x’ was not declared in this scope
     rcpp_result_gen = Rcpp::wrap(myFun(x));
                                        ^
RcppExports.cpp:16:41: error: ‘myFun’ was not declared in this scope
     rcpp_result_gen = Rcpp::wrap(myFun(x));
                                         ^
make: *** [RcppExports.o] Error 1
ERROR: compilation failed for package ‘my_package’
* removing ‘/tmp/RtmpgPjAdf/devtools_install_125071da0b53/my_package’
Error: Command failed (1)
Execution halted

Exited with status 1.

同一代码在软件包外部编译.因此,我猜测某些内容未正确复制到RcppExports文件中.在尝试使用RcppEigen命名空间时,我还注意到了一个类似的问题: using namespace RcppEigen; 未被复制.

The same code compiles outside the package. So I'm guessing that something is not copied properly to the RcppExports files. I also noticed a similar problem when trying to use the RcppEigen namespace: using namespace RcppEigen; is not copied.

有什么想法可以解决此问题而无需手动修改RcppExports吗?谢谢!

Any idea how to solve this without modifying RcppExports by hand? Thanks!

推荐答案

坚持使用 typedef 会使您的生活变得过于复杂.一旦将其包含在文件和功能签名中,它便成为接口的一部分,因此也需要位于您的 RcppExports.cpp 中.因此,您需要将其提高一级".

You are making your life too complicated by insisting on the typedef. Once you include it in the file, and your function signature, it becomes part of the interface and hence also needs to be in your RcppExports.cpp. So you need to provide it 'one level higher'.

但是有一个规定:调用文件 pkgname_types.h ,它将包含其中.我创建了一个简单的包 eigentest ,并在 src/目录中添加了一个文件 eigentest_types.h :

But there is a provision for that: call the file pkgname_types.h and it will be included. I created a simple package eigentest and added a file eigentest_types.h in the src/ directory:

#include <RcppEigen.h>

typedef Eigen::ArrayXd MapAr1;

,然后使用

#include "eigentest_types.h"

而不是 typedef .

这就是您所需要的-只需将 eigentest 替换为您的软件包名称即可.

That's all you need -- just replace eigentest with your package name.

这篇关于使用Rcpp和typedef进行编译时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆