首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RcppArmadillo和arma命名空间

RcppArmadillo和arma命名空间
EN

Stack Overflow用户
提问于 2014-02-21 20:39:35
回答 1查看 6.2K关注 0票数 9

刚开始对R有经验,但我是C++的新手,我用RcppArmadillo编写了一些函数,并且对它的可用性和速度都非常热心。现在我想使用函数RcppArmadillo.package.skeleton()将这些函数转换成一个包。

只要我在每个Armadillo对象(mat、colvec等)之前显式地使用arma::前缀,这就可以了。但是,如果我将using namespace arma;放在cpp文件的开头,然后省略arma::,则无法加载新创建的包并获得大量错误,这些错误表明Armadillo命名空间没有被识别。

任何关于如何解决这个问题的帮助/建议都非常感谢。谢谢,

法比安

PS:我在Windows 7和Ubuntu12.04上都尝试了上述方法,在每种情况下都使用了R3.0.2和RcppArmadillo_0.4.000.4。

PS2:附带的cpp文件(松散地跟随http://gallery.rcpp.org/articles/simulate-multivariate-normal/)说明了我的观点。如果我通过sourceCpp (从Rcpp包)将其导入R中,它会很好地工作,但是当我尝试将它包含到一个新包中时,会导致上面提到的问题。

代码语言:javascript
复制
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;

// [[Rcpp::export]]
colvec mvndrawC(colvec mu, mat sig) {

double k = mu.size();
colvec aux = as<colvec>(rnorm(k));
mat csig = chol(sig).t();
colvec out = mu + csig*aux;
return(out);

}

编辑:详细信息

下面是当我执行以下操作时得到的错误输出:

  1. 运行命令RcppArmadillo.package.skeleton("test2"),从而为新包"test2“创建文件夹
  2. 将上述代码粘贴到.cpp文件中,并将其复制到新的test2/src文件夹中。
  3. 试图通过从test2包调用load_all("test2")来加载新的devtools

错误消息(在Rstudio中)

代码语言:javascript
复制
Loading test2
Re-compiling test2
'/usr/lib/R/bin/R' --vanilla CMD INSTALL '/home/fabian/test2' --library='/tmp    
/RtmplfAET0'  \
--no-R --no-data --no-help --no-demo --no-inst --no-docs --no-exec --no-multiarch  \
--no-test-load --preclean 

* installing *source* package 'test2' ...
** libs
g++ -I/usr/share/R/include -DNDEBUG   -I"/home/fabian/R/x86_64-pc-linux-gnu-library   
/3.0/Rcpp/include" -I"/home/fabian/R/x86_64-pc-linux-gnu-library/3.0/RcppArmadillo  
/include"  -UNDEBUG -Wall -pedantic -g -O0 -fpic  -O3 -pipe  -g  -c RcppExports.cpp -o 
RcppExports.o
RcppExports.cpp:10:1: error: 'colvec' does not name a type
RcppExports.cpp: In function 'SEXPREC* test2_mvndrawC(SEXP, SEXP)':
RcppExports.cpp:16:40: error: 'colvec' was not declared in this scope
RcppExports.cpp:16:40: note: suggested alternative:
/home/fabian/R/x86_64-pc-linux-gnu-library/3.0/RcppArmadillo/include/armadillo_bits   
/typedef_mat.hpp:65:22: note:   'arma::colvec'
RcppExports.cpp:16:47: error: template argument 1 is invalid
RcppExports.cpp:16:55: error: expected initializer before 'mu'
RcppExports.cpp:17:40: error: 'mat' was not declared in this scope
RcppExports.cpp:17:40: note: suggested alternative:
/home/fabian/R/x86_64-pc-linux-gnu-library/3.0/RcppArmadillo/include/armadillo_bits   
/typedef_mat.hpp:63:22: note:   'arma::mat'
RcppExports.cpp:17:44: error: template argument 1 is invalid
RcppExports.cpp:17:52: error: expected initializer before 'sig'
RcppExports.cpp:18:16: error: expected ';' before '__result'
RcppExports.cpp:19:9: error: '__result' was not declared in this scope
make: *** [RcppExports.o] Error 1
ERROR: compilation failed for package 'test2'
* removing '/tmp/RtmplfAET0/test2'
Error: Command failed (1)
In addition: Warning message:
The following packages are referenced using Rcpp::depends attributes however are  
not listed in the Depends and LinkingTo fields of the package DESCRIPTION file: 
RcppArmadillo 

PS3:如果我在文件开头删除// [[Rcpp...行,那么结尾处的警告消息就会消失(但是我不能通过sourceCpp获取它,所以我让它进来了)。

EN

回答 1

Stack Overflow用户

发布于 2014-02-21 21:00:43

我认为这是因为Rcpp属性将您生成的C++源文件编译为RcppExports.cpp,它不会将using namespace arma;语句复制到其中。

这很棘手,因为不同的文件可能使用不同的名称空间,因此属性解析器不能只将所有using namespace ...语句复制到RcppExports.cpp中--它只是默认使用Rcpp命名空间。如果我们只是故意地将所有using namespace语句复制到RcppExports.cpp中,肯定会发生冲突。

无论如何,修复要么显式前缀arma::,要么修改RcppExports.cpp文件,使using namespace arma;位于顶部(但是每次调用compileAttributes()之后都必须这样做)。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21944695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档