简体   繁体   中英

When compiling cpp-code wihtin R making use of the Gnu Scientific Library via RcppGSL, inclusion of some GSL-header files produce errors

I got a problem with the compilation of c++ code within R using the RcppGSL package. I'm on Unix (Ubuntu 13.10, Installed in an Oracle VM VirtualBox; R-version 3.0.2). I managed to install everything necessary for the compilation of code making use of the GSL library (at least I think so). The following code is no problem:

library(RcppGSL)
library(inline)

code <- ' 
#include<gsl/gsl_vector.h>
//#include<gsl/gsl_pow_int.h>

RcppGSL::vector<double> v(1);
v[0] = 1.23;
double ret = v[0];
v.free() ;
return(Rcpp::wrap(ret));
' 
foo <- cxxfunction(signature(), code, plugin = "RcppGSL", verbose = T)

This compiles nicely, the function returns 1.23 as expected. But as soon as I include other header files than gsl_vector.h or gsl_matrix.h (like in the upper example when gsl/gsl_pow_int.h is not commented out on line 6), I get following error message:

Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! In file included from file196520f2906c.cpp:32:0:
/usr/include/gsl/gsl_pow_int.h: In function ‘SEXPREC* file196520f2906c()’:
/usr/include/gsl/gsl_pow_int.h:34:1: error: expected unqualified-id before string constant
 __BEGIN_DECLS
 ^
make: *** [file196520f2906c.o] Error 1

I have no clue what's the matter. Any help is appreciated.

Thanks

I think you just had some basic errors here. The following variant works fine:

#include<RcppGSL.h>
#include<gsl/gsl_vector.h>
#include<gsl/gsl_pow_int.h>

// [[Rcpp::depends(RcppGSL)]]

// [[Rcpp::export]]
double foo(int ignored) {
    RcppGSL::vector<double> v(1);
    v[0] = 1.23;
    double ret = v[0];
    v.free() ;
    return ret;
}

as per a quick test:

R> sourceCpp("/tmp/so2.cpp")
R> foo(3)
[1] 1.23
R> 

There are examples: a simple but complete package (using a vector norm) is included in the RcppGSL package itself; there are also posts on the Rcpp Gallery.

And generally speaking, including our header RcppGSL.h already pulls in the GSL headers for vectors and matrices so if you include them again (which none of our examples do) you may indeed get a error -- there has to be a certain order to make the template magic work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM