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

后端 未结 2 1375
后悔当初
后悔当初 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 
    #include 
    #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.

提交回复
热议问题