调试在Visual Studio Code中使用makefile构建的c++

Debugging c++ built with a makefile in Visual Studio Code

本文关键字:makefile 构建 c++ Visual Studio Code 调试      更新时间:2023-10-16

我有一个现有的代码库,构建一个makefile,我想使用Visual Studio code来运行它。

我首先用Visual Studio Code打开我的项目目录。然后,为了构建,我按照http://code.visualstudio.com/docs/languages/cpp:

创建了一个任务。
{
    "version": "0.1.0",
    "command": "bash",
    "isShellCommand": true,
    "args":["-c", "cd build && make"]
}

可以正常工作。然后我下载了c++扩展并创建了一个launch.json:

{
"version": "0.2.0",
"configurations": [
    {
        "name": "C++ Launch",
        "type": "cppdbg",
        "request": "launch",
        "targetArchitecture": "x64",
        "program": "${workspaceRoot}/build/myProgram",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceRoot}",
        "environment": [],
        "externalConsole": true,
        "linux": {
            "MIMode": "gdb"
        },
        "osx": {
            "MIMode": "lldb"
        },
        "windows": {
            "MIMode": "gdb"
        }
    },
    // attach here, which I don't care about
]
}

这个启动文件允许我使用内置调试器启动程序。我知道它是有效的,因为我看到所有的符号文件被加载,我可以暂停程序。

然而,VS Code不"看到"我的源文件;当我暂停时,它显示

 Source /Unknown Source is not available.

我也不能在源代码中设置断点。我看到一些符号被加载,尽管当我暂停执行时,我可以在调用堆栈上看到我的函数名。

我错过了什么?我绝对要做的,因为他们说在这里(http://code.visualstudio.com/docs/languages/cpp),并使一个g++任务,我将不得不手动输入所有我的包括路径和链接器的输入?首先使用makefile的意义在于不必做这些繁琐的事情…

非常感谢那些知道如何处理VS代码的人

您应该能够通过使用tasks.jsonlaunch.json进行调试。

你确定你有-g选项设置在你的makefile?

另一件事-你可以让你的任务在启动时执行添加"preLaunchTask": "bash"到你的launch.json .

希望这对你有帮助!

At windows;Mingw, gdb-g参数在编译可执行文件时是c/c++扩展所必需的。launch.json中不需要"preLaunchTask": "bash"