How to debug an R package (with C code) in Emacs using GDB?

后端 未结 2 1370
后悔当初
后悔当初 2021-02-05 11:51

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-program

相关标签:
2条回答
  • 2021-02-05 12:13

    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.

    0 讨论(0)
  • 2021-02-05 12:32

    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).

    0 讨论(0)
提交回复
热议问题