编译 Rcpp 代码会出现分段错误

Compiling Rcpp code gives segmentation fault

我想跟踪我的 Rcpp 代码中的分段错误。为此,我想用 GDB 实现 "main" C++ 函数和 运行 我的代码。但是我无法使这个主要功能起作用。这是一个最小的例子:

#include <Rcpp.h>

int main (int argc, char** argv) {
    Rcp::NumericVector i;
    return 0;
};

我使用了Rcpp使用的标志来编译代码:g++ -I/usr/share/R/include -DNDEBUG -I"/home/login/R/x86_64-pc-linux-gnu-library/3.3/Rcpp/include" -fpic -g -ggdb -O0 -fdebug-prefix-map=/build/r-base-6WVosl/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -c main.cpp -o main.o

然后g++ -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o main main.o -L/usr/lib/R/lib -lR -g -ggdb -O0

(我尝试了几种口味:删除 fpic-Wl。)

运行 main 给我一个分段错误。

GDB 回溯给了我

#0  0x00007ffff78f5ac5 in ?? () from /usr/lib/libR.so
#1  0x00007ffff78f6a17 in ?? () from /usr/lib/libR.so
#2  0x00007ffff78f70ad in Rf_allocVector3 () from /usr/lib/libR.so
#3  0x0000555555556856 in Rcpp::Vector<14, Rcpp::PreserveStorage>::Vector (this=0x7fffffffe1e0) at /home/login/R/x86_64-pc-linux-gnu-library/3.3/Rcpp/include/Rcpp/vector/Vector.h:58
#4  0x00005555555560ec in main (argc=1, argv=0x7fffffffe2f8) at main.cpp:7

Valgrind 在同一个地方建议 Invalid read of size 8

欢迎提出任何建议。

g++ 是 (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406。电脑是 Linux XXX 4.10.0-38-generic #42-Ubuntu SMP Tue Oct 10 13:24:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux.

对于具有基础 R 会话的 Rcpp 代码,您根本无法在 main() 上使用 gdball R 对象需要 R。

所以要么用 gdb 从 R 调试你的包——如果你真的认为你需要一个 main described in Section 4.4 of Writing R Extensions or use RInside

可以使用gdb。只是不是您尝试过的方式。