Rcpp :: IntegerVector创建给出了分段错误

时间:2016-03-02 10:03:23

标签: c++ c++11 segmentation-fault qt-creator rcpp

一些RCpp示例代码在RStudio中运行顺畅。在C ++ IDE中,尽管有新的存储库,现代的GNU / Linux发行版和现代的g ++版本,Qt Creator的分段错误(SIGSEGV)也会出现。

此代码在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,并使用Dirk Eddelbuettel和Romain Francois建议的代码:

## -*- 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下的最新版本:

richel@druten:~/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) 

我的发行版:

richel@druten:~/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() ;
}

可能出了什么问题?

2 个答案:

答案 0 :(得分:1)

您需要显示您的代码,即a minimally reproducible example

另请注意

  • CMake和QMake不受官方支持
  • 在他写作时,彼得工作的RInside中提供的CMake文件
  • 你的帖子并不清楚你在做什么;你不能只把Rcpp放到你的主程序中。

rcpp-devel列表可以进行更多的扩展讨论,可能更多的了解你的眼睛。

答案 1 :(得分:0)

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

当我尝试重现最低限度可重复的示例时,我发现我需要从命令行安装Rcpp,以便编译C ++ 11代码。

然后我退出生成一个最低限度可重复的示例来确认这一点,因为我开始使用C ++ 11和Rcpp部分(https://github.com/richelbilderbeek/travis_cpp_tutorial)编写教程,其中的示例都非常有效。