Jul*_*uet 5 c++ linker r armadillo rcpp
我试图用非常最近RcppArmadillo包的能力(版本0.3.910.0有R 3.0.1和evrerything最新)从矩阵封装(类"dgCMatrix")的稀疏矩阵转换到sp_mat类型犰狳.我正在使用文件"RcppArmadilloExtensions/spmat.h"中的"as"和"wrap"函数.不幸的是,在尝试创建共享库时遇到编译错误.so在调用"R CMD INSTALL myRpackage"时.
这是重现错误的最小示例:
文件"arma_sp_sum.h"
#ifndef _anRpackage_ARMA_SP_SUM_H
#define _anRpackage_ARMA_SP_SUM_H
#include <RcppArmadilloExtensions/spmat.h>
RcppExport SEXP arma_sp_prod(SEXP SPMAT) ;
#endif
Run Code Online (Sandbox Code Playgroud)
文件"arma_sp_sum.cpp"
#include "arma_sp_sum.h"
using namespace Rcpp ;
SEXP arma_sp_sum(SEXP SPMAT){
arma::sp_mat m1 = Rcpp::as<arma::sp_mat>(SPMAT) ;
arma::sp_mat m2 = Rcpp::as<arma::sp_mat>(SPMAT) ;
arma::sp_mat res = m1 + m2;
return Rcpp::wrap(res) ;
}
Run Code Online (Sandbox Code Playgroud)
文件"arma_sp_prod.h"
#ifndef _anRpackage_ARMA_SP_PROD_H
#define _anRpackage_ARMA_SP_PROD_H
#include <RcppArmadilloExtensions/spmat.h>
RcppExport SEXP arma_sp_prod(SEXP SPMAT) ;
#endif
Run Code Online (Sandbox Code Playgroud)
档案"arma_sp_prod.cpp"
#include "arma_sp_prod.h"
using namespace Rcpp ;
SEXP arma_sp_prod(SEXP SPMAT){
arma::sp_mat m1 = Rcpp::as<arma::sp_mat>(SPMAT) ;
arma::sp_mat m2 = Rcpp::as<arma::sp_mat>(SPMAT) ;
arma::sp_mat res = m1 * m2;
return Rcpp::wrap(res) ;
}
Run Code Online (Sandbox Code Playgroud)
然后,当运行$ R CMD INSTALL anRpackage $时,编译器会连续创建".o"文件,但是我得到以下ld错误:
ld: duplicate symbol arma::SpMat<double> Rcpp::as<arma::SpMat<double> >(SEXPREC*)in arma_sp_sum.o and arma_sp_prod.o for architecture x86_64
collect2: ld returned 1 exit status
make: *** [anRpackage.so] Error 1
ERROR: compilation failed for package ‘anRpackage’
Run Code Online (Sandbox Code Playgroud)
那么我做错了什么?对不起,如果这是一个愚蠢的问题......无论如何,感谢所有在armadilllo/RcppArmadillo做得很好的人,并感谢你的帮助.
J.
我做了一些更改来RcppArmadillo清理它。现在as和已wrap针对armadillo( arma::SpMat<T>) 中的稀疏矩阵类型正确模板化。
RcppArmadillo您可以使用from svn再试一次吗?
另外,现在你应该只需要
#include <RcppArmadillo.h>
Run Code Online (Sandbox Code Playgroud)
使用更新的代码,我可以编译您的包以及类似的内容:
#include <RcppArmadillo.h>
// [[Rcpp::depends("RcppArmadillo")]]
using namespace Rcpp ;
// [[Rcpp::export]]
arma::sp_mat sparse( arma::sp_mat A ){
A(0,0) = 1;
A(1,0) = 2;
return A ;
}
/*** R
require(Matrix)
m <- Matrix(c(0,0,2:0), 3,5)
sparse(m)
*/
Run Code Online (Sandbox Code Playgroud)