简体   繁体   English

在通过RcppGSL使用Gnu科学库编译cpp代码wihtin R时,包含某些GSL头文件会产生错误

[英]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. 使用RcppGSL包在R中编译c ++代码时遇到问题。 I'm on Unix (Ubuntu 13.10, Installed in an Oracle VM VirtualBox; R-version 3.0.2). 我在Unix上(Ubuntu 13.10,安装在Oracle VM VirtualBox中; R版本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). 我设法利用GSL库安装了所有代码汇编所需的东西(至少我是这样认为的)。 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. 这样可以很好地编译,该函数按预期返回1.23。 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: 但是,一旦我包含gsl_vector.h或gsl_matrix.h之外的其他头文件(例如在上例中,当gsl / gsl_pow_int.h在第6行未注释掉时),我得到以下错误消息:

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; 有示例:RcppGSL程序包本身包含一个简单但完整的程序包(使用矢量规范); there are also posts on the Rcpp Gallery. 在Rcpp画廊上也有帖子。

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. 通常来说,包括我们的头文件RcppGSL.h已经为向量和矩阵RcppGSL.h了GSL头文件,因此,如果再次包含它们(我们的示例都没有),则可能确实会出错-必须有一定的顺序使模板魔术工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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