提问者:小点点

如何用vscode调试c语言中的“externer'”


我不熟悉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体系结构x86_64的未定义符号:“_write_external”,引用自: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"
}

如何配置这个?


共1个答案

匿名用户

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

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

相关问题