2016-03-02 47 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)開發一個教程,其中的例子都非常棒。