2011-11-09 54 views
4

我想从Windows上的C++调用R函数。我使用MinGW来编译程序,但它在编译时会引发错误。均码(从德克拍摄)和编译错误如下:在Windows上从C++调用R函数

#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 M\n'); 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:\ProgramFiles\R\R-2.14.0\library\RInside\include" -I "C:\Progra 
mFiles\R\R-2.14.0\library\Rcpp\include" -I "C:\ProgramFiles\R\R-2.14.0\include" 
RFunctions.cpp -o sh1.exe 

错误:

C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x19a): und 
efined reference to `RInside::RInside(int, char const* const*, bool)' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x1ee): und 
efined reference to `RInside::operator[](std::string const&)' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x26d): und 
efined reference to `RInside::parseEval(std::string const&)' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x35b): und 
efined reference to `RInside::~RInside()' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text+0x3e1): und 
efined reference to `RInside::~RInside()' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp7RO 
bjectC2Ev[Rcpp::RObject::RObject()]+0x8): undefined reference to `vtable for Rcp 
p::RObject' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp7RO 
bjectC2Ev[Rcpp::RObject::RObject()]+0xd): undefined reference to `_imp__R_NilVal 
ue' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN7RInside 
5ProxyD1Ev[RInside::Proxy::~Proxy()]+0xd): undefined reference to `Rcpp::RObject 
::~RObject()' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve 
ctorILi14EED2Ev[Rcpp::Vector<14>::~Vector()]+0x16): undefined reference to `Rcpp 
::RObject::~RObject()' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve 
ctorILi14EED1Ev[Rcpp::Vector<14>::~Vector()]+0x16): undefined reference to `Rcpp 
::RObject::~RObject()' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve 
ctorILi14EEC1EP7SEXPREC[Rcpp::Vector<14>::Vector(SEXPREC*)]+0x57): undefined ref 
erence to `Rcpp::RObject::setSEXP(SEXPREC*)' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve 
ctorILi14EEC1EP7SEXPREC[Rcpp::Vector<14>::Vector(SEXPREC*)]+0x66): undefined ref 
erence to `Rcpp::RObject::~RObject()' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.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:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZNK4Rcpp6V 
ectorILi14EE4sizeEv[Rcpp::Vector<14>::size() const]+0x10): undefined reference t 
o `Rf_length' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6r_ 
castILi14EEEP7SEXPRECS2_[SEXPREC* Rcpp::r_cast<14>(SEXPREC*)]+0xd): undefined re 
ference to `TYPEOF' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.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:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve 
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)] 
+0x46): undefined reference to `Rcpp::Dimension::prod() const' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve 
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)] 
+0x56): undefined reference to `Rf_allocVector' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve 
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)] 
+0x67): undefined reference to `Rcpp::RObject::setSEXP(SEXPREC*)' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve 
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)] 
+0x7d): undefined reference to `Rcpp::Dimension::size() const' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.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:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp6Ve 
ctorILi14EEC2ERKNS_9DimensionE[Rcpp::Vector<14>::Vector(Rcpp::Dimension const&)] 
+0x13b): undefined reference to `Rcpp::RObject::~RObject()' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.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:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.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:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.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:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.o:RFunctions.cpp:(.text$_ZN4Rcpp8in 
ternal13r_init_vectorILi14EEEvP7SEXPREC[void Rcpp::internal::r_init_vector<14>(S 
EXPREC*)]+0x23): undefined reference to `Rf_length' 
C:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.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:\Users\ksharma\AppData\Local\Temp\ccgMgFPS.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 

任何想法我很想念?

回答

2

你实际上并没有在R库中进行链接(不管是什么)。

4

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

当使用RInside,还需要

  1. 包括并用Rcpp链路(其RInside取决于)一个

  2. 包括与链路与R(其中两个取决于)以及当然

  3. RInside自己的图书馆

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

最后,这是您最大的问题:RInside应用程序目前不在Windows上工作,这在RInside页面上有明确记录。它会建立,但在启动时发生段错误。调试帮助将不胜感激,这适用于OS X和Linux。

+1

迟来的评论:“Windows上的问题”问题已经修复了一段时间;在Windows上,RInside可以在OS X和Linux上运行。 –