在尝试在共享库中使用嵌入式R实例时,我遇到了使用Rcpp和R库的问题。
因此,例如,简单二进制文件中的此代码段将正确运行:
main.cc
#include <Rcpp.h>
int main(int argc, char **argv)
{
Rcpp::NumericVector numeric_vector;
}
编译:
make all
Building file: ../src/main.cc
Invoking: GCC C++ Compiler
g++ -I/home/mco/R/x86_64-pc-linux-gnu-library/3.0/RInside/include/ -I/home/mco/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include/ -I/usr/share/R/include/ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/main.d" -MT"src/main.d" -o "src/main.o" "../src/main.cc"
Finished building: ../src/main.cc
Building target: rcpp_standalone
Invoking: GCC C++ Linker
g++ -L/home/mco/R/x86_64-pc-linux-gnu-library/3.0/RInside/lib/ -o "rcpp_standalone" ./src/main.o -lRInside -lR
Finished building target: rcpp_standalone
所以现在我想将嵌入式R实例移动到共享库中。我有以下结构:
rcpp_shared
|_ src
|_ SharedObject.hh
|_ SharedObject.cc
|_ Debug
|_ librcpp_shared.so
rcpp_binary
|_ src
|_ main.cc
|_ Release
|_ rcpp_binary
代码如下:
SharedObject.hh
#ifndef SHAREDOBJECT_HH_
#define SHAREDOBJECT_HH_
namespace namespace_a {
class SharedObject
{
public:
SharedObject();
~SharedObject();
void do_something();
};
} // namespace namespace_a
#endif /* SHAREDOBJECT_HH_ */
SharedObject.cc
#include "SharedObject.hh"
#include <Rcpp.h>
namespace namespace_a {
SharedObject::SharedObject() {}
SharedObject::~SharedObject() {}
void SharedObject::do_something()
{
Rcpp::NumericVector numeric_vector;
}
} // namespace namespace_a
编译时使用:
make all
Building file: ../src/SharedObject.cc
Invoking: GCC C++ Compiler
g++ -I../src -I/home/mco/R/x86_64-pc-linux-gnu-library/3.0/RInside/include/ -I/home/mco/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include/ -I/usr/share/R/include/ -O0 -g3 -c -fmessage-length=0 --std=c++11 -fPIC -MMD -MP -MF"src/SharedObject.d" -MT"src/SharedObject.d" -o "src/SharedObject.o" "../src/SharedObject.cc"
Finished building: ../src/SharedObject.cc
Building target: librcpp_shared.so
Invoking: GCC C++ Linker
g++ -L/home/mco/R/x86_64-pc-linux-gnu-library/3.0/RInside/lib/ -shared -o "librcpp_shared.so" ./src/SharedObject.o -lRInside -lR
Finished building target: librcpp_shared.so
main.cc
#include "SharedObject.hh"
int main(int argc, char **argv)
{
namespace_a::SharedObject shared_object;
shared_object.do_something();
}
编译时使用:
make all
Building file: ../src/main.cc
Invoking: GCC C++ Compiler
g++ -I../src -I../../rcpp_shared/src -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/main.d" -MT"src/main.d" -o "src/main.o" "../src/main.cc"
Finished building: ../src/main.cc
Building target: rcpp_binary
Invoking: GCC C++ Linker
g++ -L../../rcpp_shared/Release -L/home/mco/R/x86_64-pc-linux-gnu-library/3.0/RInside/lib/ -o "rcpp_binary" ./src/main.o -lrcpp_shared -lRInside -lR
Finished building target: rcpp_binary
但是当运行rcpp_binary时,我遇到以下运行时错误/分段错误:
gdb ./Debug/rcpp_binary
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
[...]
Reading symbols from ./Debug/rcpp_binary...done.
(gdb) r
Starting program: /home/mco/dev/workspace/debub_rcpp/rcpp_binary/Debug/rcpp_binary
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7bd72c9 in dataptr (x=<error reading variable: Cannot access memory at address 0x7fffff7feff8>) at /home/mco/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include/Rcpp/routines.h:196
196 inline void* dataptr(SEXP x){
(gdb) bt
#0 0x00007ffff7bd72c9 in dataptr (x=<error reading variable: Cannot access memory at address 0x7fffff7feff8>) at /home/mco/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include/Rcpp/routines.h:196
#1 0x00007ffff7bd733b in dataptr (x=0x20f8938) at /home/mco/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include/Rcpp/routines.h:199
#2 0x00007ffff7bd733b in dataptr (x=0x20f8938) at /home/mco/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include/Rcpp/routines.h:199
#3 0x00007ffff7bd733b in dataptr (x=0x20f8938) at /home/mco/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include/Rcpp/routines.h:199
#4 0x00007ffff7bd733b in dataptr (x=0x20f8938) at /home/mco/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include/Rcpp/routines.h:199
#5 0x00007ffff7bd733b in dataptr (x=0x20f8938) at /home/mco/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include/Rcpp/routines.h:199
[...]
gdb跟踪就像那样永远。有没有人知道发生了什么以及如何让它发挥作用?
我删除了依赖于RInside并在我的libR.so中崩溃。
gdb ./rcpp_binary
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
[...]
Reading symbols from ./rcpp_binary...done.
(gdb) r
Starting program: /home/vagrant/test_rinside/rcpp_binary
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6eaf19d in ?? () from /usr/lib/libR.so
(gdb) bt
#0 0x00007ffff6eaf19d in ?? () from /usr/lib/libR.so
#1 0x00007ffff6eb0cd7 in Rf_allocVector () from /usr/lib/libR.so
#2 0x00007ffff7bd7552 in Rcpp::Vector<14, Rcpp::PreserveStorage>::Vector (this=0x7fffffffe3e0) at /usr/local/lib/R/site-library/Rcpp/include/Rcpp/vector/Vector.h:58
#3 0x00007ffff7bd6eca in namespace_a::SharedObject::do_something (this=0x7fffffffe41f) at src/SharedObject.cc:16
#4 0x00000000004008de in main (argc=1, argv=0x7fffffffe518) at src/main.cc:6
安装r-base-code-dgb之后,分段错误来自对GetNewPage的调用,如下所示。不知道它是否有帮助..
gdb ./rcpp_binary
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
[...]
Reading symbols from rcpp_binary...done.
(gdb) r
Starting program: /home/vagrant/test_rinside/rcpp_binary
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
GetNewPage (node_class=node_class@entry=0) at memory.c:861
861 memory.c: No such file or directory.
(gdb) bt
#0 GetNewPage (node_class=node_class@entry=0) at memory.c:861
#1 0x00007ffff6eb0cd7 in allocSExpNonCons (t=14) at memory.c:2112
#2 Rf_allocVector (type=14, length=0) at memory.c:2511
#3 0x00007ffff7bd7552 in Rcpp::Vector<14, Rcpp::PreserveStorage>::Vector (this=0x7fffffffe3e0) at /usr/local/lib/R/site-library/Rcpp/include/Rcpp/vector/Vector.h:58
#4 0x00007ffff7bd6eca in namespace_a::SharedObject::do_something (this=0x7fffffffe41f) at src/SharedObject.cc:16
#5 0x00000000004008de in main (argc=1, argv=0x7fffffffe518) at src/main.cc:6