1

私は 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 プラグインでデバッグするとエラーが表示されます

/"main アーキテクチャ x86_64 の未定義シンボル: "_write_extern"、参照元: main-217186.o ld の _main: アーキテクチャ x86_64 のシンボルが見つかりませんでした。呼び出しを参照)

私の 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"
}

これを設定する方法は?

4

1 に答える 1

2

デフォルトでは、タスクは現在開いているファイルのみをコンパイルするため、事前起動タスクを変更して、必要なものをすべてコンパイルする必要があります。次のように、このためのカスタム タスクを作成できます。

{
"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"
    }
]
于 2020-04-01T17:23:55.543 に答える