How to debug a cmake/make project in VSCode?

16,724

Solution 1

It seems you built release version of your program. Try to build debug version of your program.

rm -r build
cd build
cmake -DCMAKE_BUILD_TYPE=Debug ..
cmake --build .

It is better to separate debug and release builds.

mkdir Debug
cd Debug
cmake -DCMAKE_BUILD_TYPE=Debug ..
cmake --build .

With appropriate update of launch.json:

{
    "version": "2.0.0",
    "configurations": [
        {
            "name": "Debug",
            "type": "cppgdb",
            "request": "launch",
            "target": "./Debug/bin/CHIP8",
            "cwd": "${workspaceRoot}",
            "preLaunchTask": "build"
        }
    ]
}

Updated "type" according to VS Code updates. "type": "gdb" was previously

Solution 2

For my case, even I use cmake -DCMAKE_BUILD_TYPE=Debug .., it still cannot work, I need to set below flags in my cmakefile.

set(CMAKE_C_FLAGS_DEBUG "-g -DDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-g -DDEBUG")

Share:
16,724
Kevin
Author by

Kevin

Updated on July 25, 2022

Comments

  • Kevin
    Kevin almost 2 years

    I'm making a project and in order to assist in building, I'm using CMake.

    However, I notice that I can't debug.

    Here's my launch.json:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Debug",
                "type": "gdb",
                "request": "launch",
                "target": "./build/bin/CHIP8",
                "cwd": "${workspaceRoot}",
                "preLaunchTask": "build"
            }
        ]
    }
    

    And here's my tasks.json:

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "build",
                "type": "shell",
                "command": "cd build && cmake .. && make"
            }
        ]
    }
    

    I can't find anything online to help with this problem, so I'm really not sure where to go from here. VSCode docs has an example to debug where they use g++, but I'm using make --- so I'm not sure how to do it!

    Thanks.

  • Raleigh L.
    Raleigh L. over 2 years
    I tried this and I get an error saying: Configured debug type 'gdb' is not supported. Version info ------------------ Version: 1.63.2 Commit: 899d46d82c4c95423fb7e10e68eba52050e30ba3 Date: 2021-12-15T09:39:46.686Z Electron: 13.5.2 Chromium: 91.0.4472.164 Node.js: 14.16.0 V8: 9.1.269.39-electron.0 OS: Linux x64 5.13.0-25-generic snap
  • 273K
    273K over 2 years
    @RaleighL. MS as always broke it with updates. Try "cppdbg". If it works I will update the answer.