c++visual-studio-codedebuggingcmakegdb

How can I debug an executable generated by CMake in VS Code?


I have generated the executable Mytinyrender.exe in the build directory using CMake. The source file for this executable is located in src/main.cpp. Now I want to debug Mytinyrender.exe. But when I try to set a breakpoint in main.cpp and press F5 to debug, it directly generates a main.exe in the same directory as src/main.cpp.And I doubt it debuged the main.exe instead of what I want.

The .vscode configuration was manually set up by copying a previous VS Code tutorial. The launch.json file is:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\build\\MyTinyrender.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "targetArchitecture": "x86_64",
            "externalConsole": true,
            "internalConsoleOptions": "neverOpen",
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\LLVM\\bin\\gdb.exe",
            "preLaunchTask": "clang++",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

The task.json file is:

{
    "version": "2.0.0",
    "command": "clang++",
    "args": [
        "${file}",
        "-o",
        "${file}.exe",
        "-Wall",
        "-g",
        "-static-libgcc",
        "-fcolor-diagnostics",
        "-w",
        "--target=x86_64-w64-mingw",
        "-lws2_32",
        "-lIphlpapi",
        "-lgdi32"
    ],
    "tasks": [
        {
            "label": "clang++",
            "type": "shell",
            "group": "build",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            }
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "D:/LLVM/bin/g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:/LLVM/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "problemMatcher": {
        "owner": "c",
        "fileLocation": [
            "relative",
            "${workspaceRoot}"
        ],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}

Why did it turn out like this? And how can I debug MyTinyrender.exe?


Solution

  • Thanks comments.

    The issue was that I built the project using the terminal command cmake .., and used a copied configuration for debugging.

    I removed the previously configured .vscode folder and used the CMake plugin in VS Code to build and compile, which solved the problem.