c++debuggingcmakegdbg++

How to debug a CMake / Make project in VS Code?


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. VS Code docs has an example to debug where they use g++, but I'm using Make, so I'm not sure how to do it!


Solution

  • 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