Skip to content

Instantly share code, notes, and snippets.

@JADC362
Last active April 10, 2024 23:18
Show Gist options
  • Star 60 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save JADC362/a4425c2d05cdaadaaa71b697b674425f to your computer and use it in GitHub Desktop.
Save JADC362/a4425c2d05cdaadaaa71b697b674425f to your computer and use it in GitHub Desktop.
Debug ROS2 C++ node on VSCode (Ubuntu)

Debug ROS2 C++ node on VSCode (Ubuntu)

Description

This is a small tutorial on how to debug a ROS2 C++ node usign VSCode.

Requeriments

This implementation was done using:

Debug

Once you have your C++ code correctly implemented (at least compile), the First thing to do is to compile the package exporting the symbols (allow the breakpoints where you want to stop the code):

 - cd ros_ws
 - colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo
 - source install/setup.bash

Second, we have to launch the GDB Server for debbuging the CPP Code. Here, we will use a localhost:port for creating the server. Choose any free port that you want.

ros2 run --prefix 'gdbserver localhost:3000' package_name executable_name

Third, we have to create a launch.json on VSCode. In other words, we will create a custom debugging configuration. In our case, create a GDB client and connect to the server.

1) Open VSCode on your workspace.
2) Go to your side bar, 'Run and Debug' section.
3) Add a new configuration (Select C++ enviroment or any other)
4) On your launch.json file, put the following information

Launch.json file:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "C++ Debugger",
                "request": "launch",
                "type": "cppdbg",
                "miDebuggerServerAddress": "localhost:3000",
                "cwd": "/",
                "program": "[build-path-executable]"
            }
        ]
    }
  • name - Custom name of your debugger configuration
  • request - In this case we want to launch the client
  • type - cppdbg for c++ debugging
  • miDebuggerServerAddress - path_server:port
  • cwd - Where to find all the required files. We use root because ROS, the package, and other required files are distributed along the entire PC.
  • program - Change [build-path-executable] by your executable build file. You can find this path on the console when you launch the server.

Lastly, use the VSCode buttons and panels option to debug correctly your code.

This implementation was found by Alejandro Duarte and Jeison Garcia.

@moschmdt
Copy link

have a look at the VS Code ROS extension and the video tutorials provided. They should help you out quite a lot and debugging both nodes should be no problem either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment