【问题标题】:how to debug 'extern' in c with vscode如何使用vscode在c中调试'extern'
【发布时间】:2020-07-13 02:54:43
【问题描述】:

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

我有

main.c

#include <stdio.h>

int count;
extern void write_extern();

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

support.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 插件进行调试 错误显示

/"主要 架构 x86_64 的未定义符号: “_write_extern”,引用自: _main 在 main-217186.o ld:未找到架构 x86_64 的符号 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

我的 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"
}

如何配置?

【问题讨论】:

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


【解决方案1】:

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

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

【讨论】:

  • 谢谢,知道了。有关如何配置代码运行器插件以使其工作的任何提示?
  • 您需要更新 package.json 文件中的 code-runner.executorMap 以用于 c 的 code-runner。 c的配置通常看起来像“cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt”,您需要将其更新为“cd $dir && gcc *.c -o $fileNameWithoutExt” && $dir$fileNameWithoutExt" 使其工作。有一个很好的帖子here
猜你喜欢
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-30
  • 2017-07-18
  • 2012-05-12
  • 2021-02-15
相关资源
最近更新 更多