Skip to content

Instantly share code, notes, and snippets.

@yosoufe
Last active August 31, 2019 01:40
Show Gist options
  • Save yosoufe/dd37284b7319c484dd77e42947fc82b7 to your computer and use it in GitHub Desktop.
Save yosoufe/dd37284b7319c484dd77e42947fc82b7 to your computer and use it in GitHub Desktop.
Debug EKF Project on ubuntu machine with Visual Studio Code (VScode)

Youtube Link: https://youtu.be/ztRcKWe66sE

Clone the project if you do not have the folder project on your machine and make a build directory inside of it which all the compiled and executable files will be inside of it.

cd <your project folder>
mkdir build 

VS Code C/C++ Setup Tutorial

  • Install VS Code according to here or just by running sudo apt install code
  • Install the C/C++ extension by Microsoft for VS code by going to the extension tab on the left and search for C/C++.
  • You need some version of gcc on your machine. Usually some exists by default on ubuntu. You can check the version by gcc --version and you should get an output like the following, otherwise you need to install it like sudo apt install gcc-6 g++-6 for version 6.
gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  • Setup VS Code to build the project. You need to generate a task for vs code to build a project.
  • hit Ctrl+Shift+P
  • Search for Tasks: Configure Task and hit Enter
  • Create tasks.json file from Template and the select Others.
  • This will create a tasks.json file in .vscode folder. Now we need to modify the tasks.json file.
  • First we need to create a task to run the cmake
  • Delete the content of the tasks.json file and copy the following into it.
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cmake",
            "type": "shell",
            "options": {
                "cwd": "${workspaceRoot}/build"
            },
            "command": "cmake",
            "args": [
                "-G",
                "\"Unix Makefiles\"",
                "-DCMAKE_BUILD_TYPE=Debug",
                ".."
            ]
        },
        {
            "label": "build all",
            "type": "shell",
            "options": {
                "cwd": "${workspaceRoot}/build"
            },
            "command": "make -j4",
            "args": [
                
            ],
            "group": { 
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher":"$gcc",
            "presentation": {
                "clear": true
            }
        },
    ]
}
  • This will create two tasks one for running the cmake and one for building the project in the build directory
  • First you need to run the cmake task by Ctrl+Shift+P then Tasks:Run Task and then cmake and probably Continue the Task without scanning the output.
  • To build all you just need to hit Ctrl+Shift+B because we set the second task as default build task.
  • Now we need to setup the debugger. That needs a launch.json file.
  • Ctrl+Shift+P then search for lauch then choose Debug: Open launch.json and then choose C/C++ ... and then Default Configuration. That will create the launch.json file.
  • Change the program field to "program": "${workspaceFolder}/build/ExtendedKF", to point to the executable.
  • Now you can set a breakpoint and debug your code by moving to the debug view of the vscode on the left side and trace your errors. On the debug view, there is a green arrow on top that can runs the debugger.

During the debug you may encounter some errors that VScode cannot find the file or something. You can easily ignore them but if those bothers you, you can follow this instruction it eliminates them.

  • As you can see the debugger does not show the content of the eigen arrays very well. To get them right download the following two files somewhere in your machine. It does not need to be in the same folder.
mkdir debug && cd debug
wget https://bitbucket.org/eigen/eigen/raw/54b70baafc9288f3b72de4b4d429ddcce16de0d4/debug/gdb/printers.py
wget https://bitbucket.org/eigen/eigen/raw/54b70baafc9288f3b72de4b4d429ddcce16de0d4/debug/gdb/__init__.py

Then follow the instruction at the beggining of the printers.py If it does not work, it may because that file is written in python2 and your gdb is set up with python 3 according to here. You need to do the following:

sudo apt install 2to3
2to3 -w printers.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment