13

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!

3 Answers 3

14

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

Sign up to request clarification or add additional context in comments.

2 Comments

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
@RaleighL. MS as always broke it with updates. Try "cppdbg". If it works I will update the answer.
3

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

Comments

0

I'd suggest just using the debug capabilities provided by the CMake Tools and Makefile Tools extensions, respectively. Docs for the CMake Tools offerings can be found here and here, and for Makefile Tools, here. In short, select a target and then run it (which you can do with the extension-contribute wiew, or commands in the command palette).

Alternatively, you can write a cpptools launch configuration if you want to spell things out for particular targets. You'd need to start debugging through the Debug/Launch View, or the command palette command instead of the CMake/Makefile Tools features.

Or, if you want to write a cpptools launch configuration for all your project's targets and be able to use the CMake/Makefile Tools UI/command features, see How can I configure debug or "run without debugging" in CMake Tools (VS Code). TL;DR, CMake Tools offers a cmake.debugConfig setting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.