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

4

我正在尝试在Visual Studio Code中调试一个C程序。
在我的目录中,我有2个文件test.c和Makefile,以及包含launch和tasks json文件的.vscode文件夹。
我尝试配置这些文件已经三个小时了,在各种论坛和博客上搜索,但似乎没有什么起作用。

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

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

似乎VSCode在调试阶段无法找到我的test.c文件,即使它在同一个目录中。如果有人能向我展示正确的方法,那将是太好了。
这里,我附上了我文件夹中的内容。
launch.json

{
    // 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"
        }
    ]
}

tasks.json

{
    // 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
            }
        }
    ]
}

Makefile

all:
    gcc test.c -o ./test

test.c

#include<stdlib.h>
#include<stdio.h>

int main(){
    printf("Mandar\n");
    printf("Sadye\n");
    return 0;
}

感谢您的选择。
1个回答

8
你的配置正确,只有一个小问题:你忘记给gcc传递-g标志。因此,test程序中没有调试信息,因此gdb不知道源代码和编译后的程序之间的关系。
另外,在Makefile中的目标应指定它们所依赖的文件。你的all目标没有依赖于test.c,因此更改源代码不会导致重新编译。
以下是修复后的Makefile
all: test

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

有了这个修复,我能够在Linux上使用VSCode 1.36.1编译和调试此程序。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接