How to debug a cmake/make project in VSCode?

坚强是说给别人听的谎言 提交于 2019-12-24 10:40:02

问题


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.


回答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 ..
make

It is better to separate debug and release builds.

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

With appropriate update of launch.json:

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



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



来源:https://stackoverflow.com/questions/49583381/how-to-debug-a-cmake-make-project-in-vscode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!