Kevin
Kevin

Reputation: 1009

How to debug a cmake/make project in VSCode?

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.

Upvotes: 13

Views: 45888

Answers (2)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 39073

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

Upvotes: 12

Weidian Huang
Weidian Huang

Reputation: 2945

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")

Upvotes: 3

Related Questions