2016-11-11 1 views
0

zu nennen Tring habe ich eine C++ Funktion „file1.cpp“ genannt, die wie folgt aussieht:Fataler Fehler bei C++ Funktion von 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 
{ 
... 
} 

ich es von einer R-Funktion anrufen möchten. Um das zu tun, dass ich brauchte, um es zu kompilieren zuerst den „file1.so“ zu bekommen, die ich später in dem R-Befehl verwenden kann:

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

So 16.10 Terminal es ubuntu Ich schrieb:

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

, wenn ich mir die Eingabetaste drücke die follwing Fehlermeldung:

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> 

ich konnte nicht Lösung für diesen Fehler finden. Also habe ich versucht, die C++ Funktion von Rstudio aus aufzurufen. Ich schrieb die follwing Befehle:

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

Wenn es die Ausführung Ich werde diesen Fehler:

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

jemand eine Idee haben, wie es zu lösen? Danke im Voraus.

Mit besten Grüßen,

Antwort

2

Bitte einige der viele vorhandenen Beispiele auf RcppArmadillo lesen hier, am Rcpp Gallery oder, Gott bewahre, in der Paketdokumentation.

Sie natürlich konnte nur RcppArmadillo.package.skeleton() nennen und ein Arbeitspaket für Sie aus starten erstellt haben und platzieren Sie Ihre lokale Änderungen in

dies. Siehe:

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> 

, die schafft, _unter anderem diese Datei:

// -*- 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); 
} 

und t Hut sollte genug sein, um dich in Gang zu bringen.

+0

Es funktioniert wie Magie. Danke Dirk. Ich habe viele Tage damit verbracht, es herauszufinden. Allerdings werde ich einen neuen Fehler erhalten: fataler Fehler: boost/math/special_functions/gamma.hpp: Keine solche Datei oder Verzeichnis #include emadalamoudi

+0

Lesen Sie auf dem BH-Paket und studieren Pakete mit ihm. Kann so einfach sein wie das Hinzufügen eines einzelnen LinkingTo. Und, wenn ich darf, ** auch in der Rcpp Gallery ** deutlich erklärt. Versuchen Sie nicht, jedes Rad neu zu erfinden. –

+0

Ich werde Dirk, Vielen Dank für Ihre Hilfe. – emadalamoudi

Verwandte Themen