2016-11-11 42 views
0

調用C++函數時,我有一個C++函數稱爲「file1.cpp」,看起來像:致命錯誤特林來自R

#include <cmath> 
#include <stdio.h> 
#include <RcppArmadillo.h> 
#include <boost/math/special_functions/gamma.hpp> 
#include <mpi.h> 
#include <Rcpp.h> 
using namespace Rcpp; 
// [[Rcpp::export]] 
using namespace std; 
using namespace arma; 
using namespace boost::math; 
const double PIVAL = std::acos(0.0)*2; 
class function1 
{ 
... 
} 
extern "C" 
void functin2 
{ 
... 
} 

我想從R函數調用它。爲了做到這一點,我需要先編譯它獲得「file1.so」,我可以用它後來在R命令:

dyn.load("file1.so.so") 

所以,Ubuntu的16.10終端我寫道:

$ R CMD SHLIB file1.cpp -O2 -larmadillo -llapack -lblas 

當我按下進入我會得到follwing錯誤消息:

g++ -I/usr/share/R/include -DNDEBUG  -fpic -g -O2 -fdebug-prefix-map=/build/r-base-rAT5Oi/r-base-3.3.1=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c file1.cpp -o file1.o 
file1.cpp:12:81: fatal error: RcppArmadillo.h: No such file or directory 
#include <RcppArmadillo.h> 

我無法找到該錯誤的解決方案。所以,我試圖從Rstudio內部調用C++函數。我寫的follwing命令:

library(Rcpp) 
library(RcppArmadillo) 
sourceCpp("file1.cpp") 
function2() 

當執行它,我會得到這個錯誤:

file1.cpp:11:81: fatal error: RcppArmadillo.h: No such file or directory 

任何人有關於如何解決它的想法?提前致謝。

最好的問候,

回答

2

請閱讀一些關於RcppArmadillo這裏許多現有的例子,在Rcpp Gallery或者,但願,包文檔。

當然可以就叫RcppArmadillo.package.skeleton()併爲您創建工作包從開始,把你的局部變化在

看到這個:

R> setwd("/tmp") 
R> RcppArmadillo::RcppArmadillo.package.skeleton("demoPkg") 

Calling kitten to create basic package. 
Creating directories ... 
Creating DESCRIPTION ... 
Creating NAMESPACE ... 
Creating Read-and-delete-me ... 
Saving functions and data ... 
Making help files ... 
Done. 
Further steps are described in './demoPkg/Read-and-delete-me'. 

Adding pkgKitten overrides. 
Deleted 'Read-and-delete-me'. 
Done. 

Consider reading the documentation for all the packaging details. 
A good start is the 'Writing R Extensions' manual. 

And run 'R CMD check'. Run it frequently. And think of those kittens. 


Adding RcppArmadillo settings 
>> added Imports: Rcpp 
>> added LinkingTo: Rcpp, RcppArmadillo 
>> added useDynLib and importFrom directives to NAMESPACE 
>> added Makevars file with Rcpp settings 
>> added Makevars.win file with RcppArmadillo settings 
>> added example src file using armadillo classes 
>> added example Rd file for using armadillo classes 
>> invoked Rcpp::compileAttributes to create wrappers 
R> 

它創建, _特別是,這個文件:

// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 

// we only include RcppArmadillo.h which pulls Rcpp.h in for us 
#include "RcppArmadillo.h" 

// via the depends attribute we tell Rcpp to create hooks for 
// RcppArmadillo so that the build process will know what to do 
// 
// [[Rcpp::depends(RcppArmadillo)]] 

// simple example of creating two matrices and 
// returning the result of an operatioon on them 
// 
// via the exports attribute we tell Rcpp to make this function 
// available from R 
// 
// [[Rcpp::export]] 
arma::mat rcpparma_hello_world() { 
    arma::mat m1 = arma::eye<arma::mat>(3, 3); 
    arma::mat m2 = arma::eye<arma::mat>(3, 3); 

    return m1 + 3 * (m1 + m2); 
} 


// another simple example: outer product of a vector, 
// returning a matrix 
// 
// [[Rcpp::export]] 
arma::mat rcpparma_outerproduct(const arma::colvec & x) { 
    arma::mat m = x * x.t(); 
    return m; 
} 

// and the inner product returns a scalar 
// 
// [[Rcpp::export]] 
double rcpparma_innerproduct(const arma::colvec & x) { 
    double v = arma::as_scalar(x.t() * x); 
    return v; 
} 


// and we can use Rcpp::List to return both at the same time 
// 
// [[Rcpp::export]] 
Rcpp::List rcpparma_bothproducts(const arma::colvec & x) { 
    arma::mat op = x * x.t(); 
    double ip = arma::as_scalar(x.t() * x); 
    return Rcpp::List::create(Rcpp::Named("outer")=op, 
           Rcpp::Named("inner")=ip); 
} 

和t帽子應該足以讓你去。

+0

它的作品很神奇。謝謝德克。我花了很多天的時間來弄清楚。但是,我會得到一個新的錯誤:致命錯誤:boost/math/special_functions/gamma.hpp:沒有這樣的文件或目錄 #include emadalamoudi

+0

閱讀BH包和研究使用它的包。可能會像添加單個LinkingTo一樣簡單。而且,如果我可以的話,**也在Rcpp圖庫中清楚地解釋過**。不要試圖重新發明每一個輪子。 –

+0

我會德克,非常感謝您的幫助。 – emadalamoudi