Utilisation de Shlib pour compiler et charger la fonction RCPP autonome
Question
J'essaie de compiler la fonction suivante avec SHLIB
(enregistré comme foo.cpp
):
#include <Rcpp.h>
RcppExport SEXP foo( SEXP x, SEXP y){
Rcpp::NumericVector xx(x), yy(y) ;
int n = xx.size() ;
Rcpp::NumericVector res( n ) ;
double x_ = 0.0, y_ = 0.0 ;
for( int i=0; i<n; i++){
x_ = xx[i] ;
y_ = yy[i] ;
if( x_ < y_ ){
res[i] = x_ * x_ ;
} else {
res[i] = -( y_ * y_) ;
}
}
return res ;
}
J'essaye
$ R CMD SHLIB foo.cpp
/opt/local/bin/g++-mp-4.4 -I/opt/local/lib/R/include -I/opt/local/lib/R/include/x86_64 -I/opt/local/include -fPIC -pipe -O2 -m64 -c foo.cpp -o foo.o
foo.cpp:1:18: error: Rcpp.h: No such file or directory
foo.cpp:3: error: 'RcppExport' does not name a type
make: *** [foo.o] Error 1
Comment inclure ce fichier, et est-ce la bonne façon de compiler une fonction autonome avec RCPP? Bien sûr, j'ai installé RCPP avec install.packages('Rcpp')
.
Mise à jour:Essayer de trouver l'emplacement de rcpp.h dans RI Get:
> system.file("lib", "Rcpp.h", package="Rcpp")
[1] ""
>
Cependant,
> Rcpp:::LdFlags()
/opt/local/lib/R/library/Rcpp/lib/x86_64/libRcpp.a>
Mise à jour 2:
Regarder http://www.mail-archive.com/r-help@r-project.org/msg79185.html, J'ai essayé
$ PKG_CPPFLAGS=`Rscript -e 'Rcpp:::CxxFlags()'` \
> PKG_LIBS=`Rscript -e 'Rcpp:::LdFlags()'` \
> R CMD SHLIB foo.cpp
/opt/local/bin/g++-mp-4.4 -I/opt/local/lib/R/include -I/opt/local/lib/R/include/x86_64 -I/opt/local/lib/R/library/Rcpp/include -I/opt/local/include -fPIC -pipe -O2 -m64 -c foo.cpp -o foo.o
/opt/local/bin/g++-mp-4.4 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/opt/local/lib -o foo.so foo.o /opt/local/lib/R/library/Rcpp/lib/x86_64/libRcpp.a -L/opt/local/lib/R/lib/x86_64 -lR
Et il a généré foo.o
et foo.so
. Comment importer cela dans R maintenant?
Mise à jour 3:Donc il peut être chargé à partir de dyn.load
comme
> dyn.load("foo.so")
> is.loaded("foo")
[1] TRUE
Il peut être appelé avec succès comme comme
> .Call("foo",x=as.numeric(c(1,2,3)),y=as.numeric(c(4,5,6)))
[1] 1 4 9
Bien que la fonction ne soit pas visible en tant que telle.
> foo
Error: object 'foo' not found
Pas de solution correcte
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow