如何使用GDB在Emacs中调试R包(带有C代码)? [英] How to debug an R package (with C code) in Emacs using GDB?
问题描述
我目前正在编写一个R包,并通过R中的Rcpp
包使用经过编译的C ++代码(Rcpp使R和C ++代码的交互对于像我这样的非程序员来说更容易,恕我直言).
I am currently writing an R package and using compiled C++ code through the Rcpp
package in R (Rcpp makes the interaction of R and C++ code easier for a non-programmer like me, IMHO).
我想使用gdb调试C ++程序中的一些错误.我在Google上进行了搜索,发现主要是一些有关在emacs中调试R的资源,此处,当然还有R的《写作R扩展手册》.
I want to debug a few errors in my C++ program using gdb. I have googled and found mainly a few resources on debugging R within emacs, R-FAQ, a few mails here, and definitely the R's Writing R Extension Manual.
但是,这是我第一次这样做,我走得太远了.谁能给我一些关于如何在emacs中调试R包(或带有C ++/C代码的扩展)的指导.具体来说,我想利用将ESS与R结合使用以及将gdb与Emacs结合使用的优势(如R-FAQ所讨论的那样).
However, I am doing this for the first time, I could not go too far. Could anyone give me a few pointers on how to debug R packages (or extensions with C++/C code) within emacs. Specifically, I want to take advantages of using ESS with R and gdb with Emacs (as the R-FAQ talks about).
请注意,我可以确定如何仅使用 C或C ++程序使用gdb.但是我无法将这些知识转化为使用带有R和扩展名的gdb.
Please note, I am ok on how to use gdb using only C or C++ programs. But I could not translate this knowledge to using gdb with R and extensions.
推荐答案
您可以利用RInside(Rcpp的强大伴侣)将问题变成纯C ++开发和调试任务,从而充分利用现有的调试C ++程序的知识.
You can leverage your existing knowledge of debugging C++ programs by turning the problem into a pure C++ development and debugging task using RInside (a great companion to Rcpp).
编写一个main()
C ++函数,该函数使用RInside创建R实例,执行设置测试用例的R代码(或提供R脚本),然后从main()调用被测函数,例如>
Write a main()
C++ function that creates an R instance using RInside, executes R code (or sources an R script) that sets up the test case, and then call the function under test from main(), e.g.
#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;
}
然后,通过在function_under_test()
等中设置断点,使用gdb调试C ++程序时照常进行.
Then proceed as usual when debugging a C++ program with gdb by setting breakpoints in function_under_test()
etc.
这样,您可以避免在R和C ++开发环境之间切换,而不必重新安装R软件包.
This way you avoid switching between R and C++ development environments and having to re-install the R package.
这篇关于如何使用GDB在Emacs中调试R包(带有C代码)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!