gpt4 book ai didi

c++ - 使用带有 typedef 的 Rcpp 的编译错误

转载 作者:太空狗 更新时间:2023-10-29 23:49:19 24 4
gpt4 key购买 nike

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

// [[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; is not copyed.

知道如何在不手动修改 RcppExports 的情况下解决这个问题吗?谢谢!

最佳答案

你坚持使用 typedef 会让你的生活变得太复杂。一旦您将它包含在文件中,和您的函数签名,它就会成为界面的一部分,因此也需要在您的 RcppExports.cpp 中。因此,您需要提供“更高一级”。

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

#include <RcppEigen.h>

typedef Eigen::ArrayXd MapAr1;

然后用

将它添加到你的代码片段中
#include "eigentest_types.h"

而不是 typedef

这就是您所需要的——只需将 eigentest 替换为您的包名称即可。

关于c++ - 使用带有 typedef 的 Rcpp 的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42884946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com