加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

在Windows上从C调用R函数

发布时间:2020-12-14 04:31:07 所属栏目:Windows 来源:网络整理
导读:我试图在 Windows上从C调用R函数.我正在使用MinGW来编译程序,但它在编译时会抛出错误.代码(取自Dirk)和编译错误如下: #include iostreamusing namespace std;#include "RInside.h" // for the embedded R via RInsideRcpp::NumericMatrix createMatrix(cons
我试图在 Windows上从C调用R函数.我正在使用MinGW来编译程序,但它在编译时会抛出错误.代码(取自Dirk)和编译错误如下:

#include <iostream>
using namespace std;
#include "RInside.h"  // for the embedded R via RInside


Rcpp::NumericMatrix createMatrix(const int n) {
  Rcpp::NumericMatrix M(n,n);
  for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
      M(i,j) = i*10+j;
    }
  }
  return(M);
}

int main(int argc,char *argv[]) {
  const int mdim = 4;                         // let the matrices be 4 by 4
  SEXP ans;


  RInside R(argc,argv);                      // create an embedded R instance

  Rcpp::NumericMatrix M = createMatrix(mdim); // create and fill a sample data Matrix
  R["M"] = M;                                 // assign C++ matrix M to R's 'M' var

  std::string evalstr = "
            cat('Running ls()n'); print(ls());                    
            cat('Showing Mn'); print(M);                          
            cat('Showing colSums()n'); Z <- colSums(M); print(Z); 
            Z";                     // returns Z

  ans = R.parseEval(evalstr);                 // eval the init string -- Z is now in ans

  Rcpp::NumericVector v(ans);                 // convert SEXP ans to a vector of doubles
  for (int i=0; i< v.size(); i++) {           // show the result
    std::cout << "In C++ element " << i << " is " << v[i] << std::endl;
  }

  return 0;
}

编译:

g++ -I "C:ProgramFilesRR-2.14.0libraryRInsideinclude" -I "C:Progra
mFilesRR-2.14.0libraryRcppinclude" -I "C:ProgramFilesRR-2.14.0include"
RFunctions.cpp -o sh1.exe

错误:

C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text+0x19a): und
efined reference to `RInside::RInside(int,char const* const*,bool)'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text+0x1ee): und
efined reference to `RInside::operator[](std::string const&)'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text+0x26d): und
efined reference to `RInside::parseEval(std::string const&)'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text+0x35b): und
efined reference to `RInside::~RInside()'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text+0x3e1): und
efined reference to `RInside::~RInside()'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp7RO
bjectC2Ev[Rcpp::RObject::RObject()]+0x8): undefined reference to `vtable for Rcp
p::RObject'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp7RO
bjectC2Ev[Rcpp::RObject::RObject()]+0xd): undefined reference to `_imp__R_NilVal
ue'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN7RInside
5ProxyD1Ev[RInside::Proxy::~Proxy()]+0xd): undefined reference to `Rcpp::RObject
::~RObject()'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EED2Ev[Rcpp::Vector<14>::~Vector()]+0x16): undefined reference to `Rcpp
::RObject::~RObject()'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EED1Ev[Rcpp::Vector<14>::~Vector()]+0x16): undefined reference to `Rcpp
::RObject::~RObject()'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC1EP7SEXPREC[Rcpp::Vector<14>::Vector(SEXPREC*)]+0x57): undefined ref
erence to `Rcpp::RObject::setSEXP(SEXPREC*)'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC1EP7SEXPREC[Rcpp::Vector<14>::Vector(SEXPREC*)]+0x66): undefined ref
erence to `Rcpp::RObject::~RObject()'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ma
trixILi14EEC1ERKiS3_[Rcpp::Matrix<14>::Matrix(int const&,int const&)]+0x2c): un
defined reference to `Rcpp::Dimension::Dimension(unsigned int const&,unsigned i
nt const&)'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZNK4Rcpp6V
ectorILi14EE4sizeEv[Rcpp::Vector<14>::size() const]+0x10): undefined reference t
o `Rf_length'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6r_
castILi14EEEP7SEXPRECS2_[SEXPREC* Rcpp::r_cast<14>(SEXPREC*)]+0xd): undefined re
ference to `TYPEOF'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6r_
castILi14EEEP7SEXPRECS2_[SEXPREC* Rcpp::r_cast<14>(SEXPREC*)]+0x1d): undefined r
eference to `SEXPREC* Rcpp::internal::r_true_cast<14>(SEXPREC*)'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x46): undefined reference to `Rcpp::Dimension::prod() const'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x56): undefined reference to `Rf_allocVector'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x67): undefined reference to `Rcpp::RObject::setSEXP(SEXPREC*)'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x7d): undefined reference to `Rcpp::Dimension::size() const'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0xc9): undefined reference to `Rcpp::RObject::attr(std::string const&) const'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)]
+0x13b): undefined reference to `Rcpp::RObject::~RObject()'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZNK4Rcpp11
Environment6assignINS_6MatrixILi14EEEEEbRKSsRKT_[bool Rcpp::Environment::assign<
Rcpp::Matrix<14> >(std::basic_string<char,std::char_traits<char>,std::allocato
r<char> > const&,Rcpp::Matrix<14> const&) const]+0x23): undefined reference to
`Rcpp::Environment::assign(std::string const&,SEXPREC*) const'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp7RO
bject14AttributeProxyaSINS_9DimensionEEERS1_RKT_[Rcpp::RObject::AttributeProxy&
Rcpp::RObject::AttributeProxy::operator=<Rcpp::Dimension>(Rcpp::Dimension const&
)]+0x1c): undefined reference to `Rcpp::RObject::AttributeProxy::set(SEXPREC*) c
onst'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp8in
ternal13r_init_vectorILi14EEEvP7SEXPREC[void Rcpp::internal::r_init_vector<14>(S
EXPREC*)]+0xd): undefined reference to `double* Rcpp::internal::r_vector_start<1
4,double>(SEXPREC*)'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp8in
ternal13r_init_vectorILi14EEEvP7SEXPREC[void Rcpp::internal::r_init_vector<14>(S
EXPREC*)]+0x23): undefined reference to `Rf_length'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp8in
ternal21wrap_dispatch_unknownINS_9DimensionEEEP7SEXPRECRKT_NS_6traits17integral_
constantIbLb1EEE[SEXPREC* Rcpp::internal::wrap_dispatch_unknown<Rcpp::Dimension>
(Rcpp::Dimension const&,Rcpp::traits::integral_constant<bool,true>)]+0xd): und
efined reference to `Rcpp::Dimension::operator SEXPREC*() const'
C:UsersksharmaAppDataLocalTempccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6tr
aits14r_vector_cacheILi14EE6updateERKNS_6VectorILi14EEE[Rcpp::traits::r_vector_c
ache<14>::update(Rcpp::Vector<14> const&)]+0x15): undefined reference to `double
* Rcpp::internal::r_vector_start<14,double>(SEXPREC*)'
collect2: ld returned 1 exit status

我缺少什么想法?

解决方法

吉姆在他之前的回答中是正确的,但还有更多.

使用RInside时,您还需要

>包含并链接Rcpp(RInside依赖)a
>包括和链接R(两者都取决于)以及当然
> RInside自己的图书馆

最简单的方法是简单地使用examples / standard目录中的Makefile —假设您复制了其中一个示例的代码,您还应该复制构建指令.

最后,这是您最大的问题:RInside应用程序目前无法在Windows上运行,这在RInside页面上有明确说明.它会构建,但启动时会出现段错误.调试帮助将不胜感激,这适用于OS X和Linux.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读