c - 如何使用vscode在c中调试 'extern'

标签 c visual-studio-code vscode-code-runner

我不熟悉c编译器,我知道如何在终端中使用gcc或g++

我有

main.c

#include <stdio.h>

int count;
extern void write_extern();

int main()
{
   count = 5;
   write_extern();
}

支持.c

#include <stdio.h>

extern int count;

void write_extern(void)
{
   printf("count is %d\n", count);
}

gcc main.c support.c

输出文件 a.out 工作正常

但是如果我使用 vscode 或 code-runner 插件进行调试 错误显示

/"main Undefined symbols for architecture x86_64: "_write_extern", referenced from: _main in main-217186.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

我的launch.json和task.json看起来像这样:

 "configurations": [
        {
            "name": "clang build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "clang build active file"
        }
    ]
{
    "tasks": [
        {
            "type": "shell",
            "label": "clang build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "/usr/bin"
            }
        }
    ],
    "version": "2.0.0"
}

如何配置?

最佳答案

默认情况下,该任务仅编译当前打开的文件,因此您需要更改预启动任务以编译您需要的所有内容。您可以为此创建自定义任务,如下所示:

{
"tasks": [
    {
        "type": "shell",
        "label": "clang build active file",
        "command": "/usr/bin/clang",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "/usr/bin"
        }
    },
    {
        "type": "shell",
        "label": "clang build custom",
        "command": "/usr/bin/clang",
        "args": [
            "-g",
            "${fileDirname}/main.c",
            "${fileDirname}/support.c",
            "-o",
            "${fileDirname}/main"
        ],
        "options": {
            "cwd": "/usr/bin"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": "build"
    }
],
"version": "2.0.0"
}

然后更新您的 launch.json 以使用新任务:

 "configurations": [
    {
        "name": "clang build and debug custom project",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "lldb",
        "preLaunchTask": "clang build custom"
    }
]

关于c - 如何使用vscode在c中调试 'extern',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60969505/

相关文章:

c - 在 C 中选择字符串数组的随机值

c - 如何正确退出子进程?

linux - 如何从 Windows 上的 Linux 子系统打开 VS Code IDE?

visual-studio-code - 如何让 VS Code 接受用户的输入?

在C中递归创建并遍历二叉树

向函数调用 typedef 结构?

python - 如何在 VS Code 中添加多个 Python 交互窗口?

python - 如何在 Debug模式下更新 Visual Studio Code 使用的 ptvsd

c++ - VSCode 中的 Cpp - 如何编译到其他文件夹

c# - Visual Studio Code 无法从 C# 代码获取输出