问题
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).
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.
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).
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.
回答1:
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).
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;
}
Then proceed as usual when debugging a C++ program with gdb by setting breakpoints in function_under_test()
etc.
This way you avoid switching between R and C++ development environments and having to re-install the R package.
回答2:
It's not all that easy, unfortunately. You need to jump between ESS, gdb (ie gud in Emacs) and R. The best description is probably still win Writing R Extensions, however there was a recent thread on the ESS mailing list that discusses this too (and note that some replies came in outside the thread so do look at the mailing list archive too).
来源:https://stackoverflow.com/questions/4980595/how-to-debug-an-r-package-with-c-code-in-emacs-using-gdb