English 中文(简体)
如何使用GDB在Emacs中调试R包(带有C代码)?
原标题:How to debug an R package (with C code) in Emacs using GDB?
  • 时间:2011-02-12 20:59:42
  •  标签:
  • r
  • gdb
  • emacs23

我目前正在编写一个R包,并通过R中的Rcpp包使用编译的C++代码(Rcpp使像我这样的非程序员IMHO更容易进行R和C++代码的交互)。

我想使用gdb调试C++程序中的一些错误。我在谷歌上搜索了一下,主要找到了一些关于在emacs中调试R的资源,R-FAQ,几封邮件这里,当然还有R的写作R扩展手册。

然而,我是第一次这样做,我不能走得太远。有人能给我一些关于如何在emacs中调试R包(或带有C++/C代码的扩展)的建议吗。具体来说,我想利用ESS与R结合使用和gdb与Emacs结合使用的优势(正如R-FAQ所说)。

请注意,我同意如何使用gdb,只使用C或C++程序。但我无法将这些知识转化为使用带有R和扩展的gdb。

问题回答

您可以利用现有的调试C++程序的知识,使用RInside(Rcpp的一个很好的伴侣)将问题转化为纯粹的C++开发和调试任务。

编写一个main()C++函数,该函数使用RInside创建一个R实例,执行设置测试用例的R代码(或源代码为R脚本),然后从main()调用测试中的函数,例如。

#include <Rcpp.h>
#include <RInside.h>
#include "function_under_test.h"

int main(int argc, char *argv[]) 
{
    using namespace std;
    using namespace Rcpp;

    RInside R(argc, argv);

    string evalstr = R"(
        a <- matrix(c(1,1,1, 1,1,1, 1,1,1), nrow = 3, ncol=3)
    )";
    R.parseEvalQ(evalstr);

    SEXP a = R["a"];

    R["b"] = function_under_test(a);

    evalstr = R"(
        print(b)
    )";
    R.parseEvalQ(evalstr);

    return 0;
}

然后,在使用gdb调试C++程序时,如往常一样,在function_under_test()等中设置断点。

这样就可以避免在R和C++开发环境之间切换,并避免重新安装R包。

不幸的是,这并不那么容易。你需要在ESS、gdb(即Emacs中的gud)和R之间切换。最好的描述可能仍然是写R扩展,但有一个”http://news.gmane.org/gmane.emacs.ess.general“rel=”nofollow“>ESS邮件列表也讨论了这一点(请注意,有些回复来自线程之外,所以也要查看邮件列表存档)。





相关问题
tell gdb to disassemble "unknown" code

is it possible to configure gdb in order to debug assembly code when there are no debug symbols or no sources available ? I mean showing assembly instruction by assembly instruction when performing a ...

C++ Netbeans debugging issues

I installed netbeans6.7.1 ide for c/c++ also i have mingw/msys cygwin installed and i have given C:Msysin as environment variable path.It has gdb7 version.However wheni run dbugging thru netbeans ...

setting strings in gdb

c++: int main() { string a = "a"; ... ... } when i debug in gdb: (gdb) set var a = "ok" Invalid cast I run the program and pause at a break point after string a has been ...

How to debug programms written in fasm under linux using gdb?

I wrote simple "hello, world" in fasm, and its works, but how i can generate debug info for gdb and edb (Evan s Debugger)? Fasm compiler could only produce debugging symbols in its specific format - "...

How can I display Unicode strings while debugging on linux?

I have been working for some years now as C++ Developer using MS Visual Studio as working platform. Since I privately prefer to use linux, I recently took the chance to move my working environment to ...

Continue to debug after failed assertion on Linux?

When an assertion fails with Visual C++ on Windows, the debugger stops, displays the message, and then lets you continue (or, if no debugging session is running, offers to launch visual studio for you)...

热门标签