Ubuntu 如何设置Visual Studio代码以在Linux上调试C程序?

Ubuntu 如何设置Visual Studio代码以在Linux上调试C程序?,ubuntu,gcc,visual-studio-code,gdb,Ubuntu,Gcc,Visual Studio Code,Gdb,我正在尝试用Visual Studio代码调试C程序。 在我的目录中,我有两个文件test.c和Makefile以及包含启动和任务json文件的.vscode文件夹。 在过去的三个小时里,我试图在各种论坛和博客上配置这些文件,但似乎没有任何效果 我能够使用这两个json文件编译和运行。 程序正确运行并显示输出,但不会在断点处停止,在程序执行期间,我无法添加断点,已添加的断点将被禁用,并显示以下消息 包含此断点的模块尚未加载或无法获取断点地址 在调试阶段,VSCode似乎无法找到我的test.c文

我正在尝试用Visual Studio代码调试C程序。 在我的目录中,我有两个文件test.c和Makefile以及包含启动和任务json文件的.vscode文件夹。 在过去的三个小时里,我试图在各种论坛和博客上配置这些文件,但似乎没有任何效果

我能够使用这两个json文件编译和运行。 程序正确运行并显示输出,但不会在断点处停止,在程序执行期间,我无法添加断点,已添加的断点将被禁用,并显示以下消息

包含此断点的模块尚未加载或无法获取断点地址

在调试阶段,VSCode似乎无法找到我的test.c文件,即使它位于同一目录中。如果有人能告诉我正确的方法,那就太好了。 在这里,我附加在我的文件夹中的文件内容。 launch.json

tasks.json

生成文件

测试c

包括 包括 int main{ printfMandar\n; printfSadye\n; 返回0; }
谢谢。

您的配置是正确的,除了一件小事:您忘记将标志传递给gcc。因此,测试程序中没有调试信息,因此gdb不知道源代码和编译程序之间的关系

此外,Makefile中的目标应该指定它们所依赖的文件。您的all目标不依赖于test.c,因此更改源代码不会导致重新编译

下面是一个固定的Makefile:

有了这个补丁,我可以在Linux上使用VSCode 1.36.1编译和调试这个程序

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/test",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "tasks",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "tasks",
            "type": "shell",
            "command": "make",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
all:
    gcc test.c -o ./test
all: test

test: test.c
        gcc -g test.c -o ./test