user1436187 user1436187 - 4 months ago 43
R Question

How to debug an R package (with C and C++ code) in Eclipse?

In Visual Studio we can use "attach to process" in order to debug the DLLs.

Is there any way to debug C/C++ code of an R package in Eclipse/StatET (i.e. something like this example of debugging external code linked to Scilab)?


The following steps work for Eclipse-CDT C/C++ (LUNA).

  1. Compile R (3.2) with debug information. Steps for downloading source using SVN can be found in R install guide.

$./configure --enable-R-shlib --with-valgrind-instrumentation=2 --with-system-valgrind-headers --CFLAGS='-g -O0 -fPIC' FFLAGS='-g -O0 -fPIC' CXXFLAGS='-g -O0 -fPIC' FCFLAGS='-g -O0 -fPIC' 
$sudo make install 

This will install R under /usr/local/lib/R.

Note: -g and -O are needed to add debug symbols and to make sure that comiple optimization do not prevent debugging.

  1. Setup proper directory structure for an R-package with C source code. Use a makefile rather than relying on Eclipse-StatET for building the project. Makes the setup more easily portable to Windows.

  2. R CMD install at the command line or using the Makefile will install the package to the user's local R library.

  3. Under Eclipse (Luna) create a "New Debug Configuration" under C/C++ Application.

  4. Under 'Main' tab:

a. select a C/C++ Application:


b. Project may point to the R-package project.

c. Check "Connect process input & output to a terminal".

  1. Under "Arguments" tab use:

--slave --vanilla --no-save

  1. Under 'Environment' tab add:

a. LD_LIBRARY_PATH: /use/local/lib/R/lib

b. R_HOME /usr/local/lib/R Select "Append environment to native environment"

  1. Under "Debugger" tab

a. Pick GDB debugger gdb

b. If 'GDB command set' is not displayed as an option, click 'Select other...' at the very bottom. Choose 'Legacy Create Process Launcher'. Doing so will now display options for picking GDB command set: Pick Standard with protocol mi.

  1. Under "source" tab

a. Add absolute path to the R directory with R source (optional) ~/Downloads/R

b. Add absolute path to the package src directory and any other dependent libraries.

  1. Click Debug.