2016-03-02 49 views
0

某些RCpp示例代码在RStudio中平滑运行。在C++ IDE中,Qt Creator分段错误(SIGSEGV)发生,尽管有新的版本库,现代的GNU/Linux发行版和现代g ++版本。Rcpp :: IntegerVector创建给出了分段错误

此代码工作RStudio下罚款,并给了我Qt Creator中分段错误:

//main.cpp 
#include <Rcpp.h> 

int main() { 
    Rcpp::IntegerVector v(3); 
} 

不只是这个代码,而且代码从RCPP测试套件像复制,例如:

DataFrame createTwo(){ 
    IntegerVector v = IntegerVector::create(1,2,3); 
     std::vector<std::string> s(3); 
     s[0] = "a"; 
     s[1] = "b"; 
     s[2] = "c"; 
     return DataFrame::create(Named("a")=v, Named("b")=s); 
} 

我使用的qmake创建一个Makefile,并使用德克Eddelbuettel和罗曼·弗朗索瓦以及建议代码:

## -*- mode: Makefile; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*- 
## 
## Qt usage example for RInside, inspired by the standard 'density 
## sliders' example for other GUI toolkits 
## 
## This file can be used across operating systems as qmake selects appropriate 
## values as needed, as do the R and R-related calls below. See the thread at 
##  http://thread.gmane.org/gmane.comp.lang.r.rcpp/4376/focus=4402 
## for discussion specific to Windows. 
## 
## Copyright (C) 2012 Dirk Eddelbuettel and Romain Francois 

## build an app based on the one headers and two source files 
TEMPLATE =  app 

## beyond the default configuration, also use SVG graphics 
QT +=   svg 

## comment this out if you need a different version of R, 
## and set set R_HOME accordingly as an environment variable 
R_HOME =  $$system(R RHOME) 
#message("R_HOME is" $$R_HOME) 

## include headers and libraries for R 
RCPPFLAGS =   $$system($$R_HOME/bin/R CMD config --cppflags) 
RLDFLAGS =  $$system($$R_HOME/bin/R CMD config --ldflags) 
RBLAS =   $$system($$R_HOME/bin/R CMD config BLAS_LIBS) 
RLAPACK =  $$system($$R_HOME/bin/R CMD config LAPACK_LIBS) 

## if you need to set an rpath to R itself, also uncomment 
RRPATH =  -Wl,-rpath,$$R_HOME/lib 

## include headers and libraries for Rcpp interface classes 
## note that RCPPLIBS will be empty with Rcpp (>= 0.11.0) and can be omitted 
RCPPINCL =  $$system($$R_HOME/bin/Rscript -e \"Rcpp:::CxxFlags\(\)\") 
RCPPLIBS =  $$system($$R_HOME/bin/Rscript -e \"Rcpp:::LdFlags\(\)\") 

## for some reason when building with Qt we get this each time 
## /usr/local/lib/R/site-library/Rcpp/include/Rcpp/module/Module_generated_ctor_signature.h:25: warning: unused parameter ‘classname 
## so we turn unused parameter warnings off 
## no longer needed with Rcpp 0.9.3 or later 
#RCPPWARNING =  -Wno-unused-parameter 

## include headers and libraries for RInside embedding classes 
RINSIDEINCL =  $$system($$R_HOME/bin/Rscript -e \"RInside:::CxxFlags\(\)\") 
RINSIDELIBS =  $$system($$R_HOME/bin/Rscript -e \"RInside:::LdFlags\(\)\") 

## compiler etc settings used in default make rules 
QMAKE_CXXFLAGS += $$RCPPWARNING $$RCPPFLAGS $$RCPPINCL $$RINSIDEINCL 
QMAKE_LIBS +=   $$RLDFLAGS $$RBLAS $$RLAPACK $$RINSIDELIBS $$RCPPLIBS 

## addition clean targets 
QMAKE_CLEAN +=  qtdensity Makefile 

## My personal additions  

QMAKE_CXXFLAGS += -Wall -Wextra -Weffc++ -std=c++11 
QT  += core 
QT  -= gui 
TEMPLATE = app 

SOURCES = main.cpp 

请注意,我使用带有QMAKE_CXXFLAGS += -std=c++11标志的C++ 11标准。删除这个并不能解决问题。

正如我所说的,代码通过了所有RStudio测试,但会崩溃与Qt Creator中分割故障相同的代码。所有Rcpp存储库都是新鲜的。

我的G ++版本也Lubuntu下的最新版本:

[email protected]:~/GitHubs/RibiLibraries$ g++ -v 
Using built-in specs. 
COLLECT_GCC=g++ 
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/5/lto-wrapper 
Target: i686-linux-gnu 
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.2.1-22ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-i386/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-i386 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-i386 --with-arch-directory=i386 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-targets=all --enable-multiarch --disable-werror --with-arch-32=i686 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu 
Thread model: posix 
gcc version 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2) 

我的发行版:

[email protected]:~/GitHubs/RibiLibraries$ uname -a 
Linux druten 4.2.0-30-generiC#36-Ubuntu SMP Fri Feb 26 00:57:19 UTC 2016 i686 athlon i686 GNU/Linux 

在这里,我有点构建一个堆栈跟踪,调试带我到平3

0 ??   0xb7d10668 
1 Rf_allocVector3   0xb7d11727 
2 Rf_allocVector   0xb7d023be 
3 Rcpp::Vector<13, Rcpp::PreserveStorage>::Vector Vector.h 129 0x804b214 <- Debugger takes me here 
4 main main.cpp 5 0x804ac40 

的代码的关键部分是:

Vector(const int& size) { 
    Storage::set__(Rf_allocVector(RTYPE, size)) ; // <-- This line 
    init() ; 
} 

什么可能出了错?

+0

你试过valgrind吗? – tobilocker

回答

1

你需要证明你的代码,即a minimally reproducible example

还要注意的是

  • CMake和QMAKE不正式支持
  • 在RInside的贡献​​CMakefile彼得工作时,他写了他们
  • 您的文章不会让你实际上是做清楚什么;你不能只将Rcpp粘贴到你的主程序中。

rcpp-devel列表有更多的扩展讨论,可能比您可能在这里获得更多的认识眼睛。

0

我不确定,但是我敢打赌错误是在库二进制文件和目标文件不同步的。

当我试图重现最低限度再现的例子,我发现我需要安装RCPP - 从用于C++ 11代码编译命令啉。

然后我放弃了生成一个最小重复性的例子来证实这一点,因为我开始使用C++ 11和Rcpp部分(https://github.com/richelbilderbeek/travis_cpp_tutorial)开发一个教程,其中的例子都非常棒。