Visual Studio Code で2つのC++ファイルを纏めてコンパイル→ビルド→デバッグに持っていく件

何だか分からんけど、動いたのでメモっておきます。

/C/Users/ebata/kese/VSC_C++_test

というディレクトリに、main.cpp とsub.cppを置く。

#include <stdio.h>

int sub(int, int);

int main() {
    int a = sub(2, 3);
    printf("%d\n", a);

    return 0;
}
#include <stdio.h>

int sub(int a, int b) {
    int c;
    c = a + b;

    return c;
}
  • 「Ctrl+Shift+P」 で、コマンドパレットを開く
  • 「C/C++: Edit configurations...」 を選択
  • 「.vscode」 フォルダの中に 「c_cpp_properties.json」 ファイルが作成される

c_cpp_properties.jsonは、以下のようになっている。

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}
  • 「Ctrl+Shift+P」 で、コマンドパレットを開く
  • 「Tasks: Configure Task」 を選択する
  • 「テンプレートから tasks.json を生成」 をクリック
  • 「Others 任意の外部コマンドを実行する例」 をクリック
  • 「.vscode」 フォルの中に 「tasks.json」 ファイルが作成される
  • 「tasks.json」 を下記のように変更する
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "sample",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "main.cpp"
            ],
            "group": "build"
        },
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe アクティブなファイルのビルド",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "sub.cpp" // ←ここがポイント
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "デバッガーによって生成されたタスク。"
        }
    ]
}
  • 「Ctrl+Shift+D」 でデバッグを実行する
  • 環境の選択のドロップダウンで、「C++ (GDB/LLDB)」を選択
  • 「.vscode」 フォルの中に 「launch.json」 ファイルが作成される
{
    // IntelliSense を使用して利用可能な属性を学べます。
    // 既存の属性の説明をホバーして表示します。
    // 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - アクティブ ファイルのビルドとデバッグ",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "gdb の再フォーマットを有効にする",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "逆アセンブリ フレーバーを Intel に設定",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe アクティブなファイルのビルド"
        }
    ]
}

こんな感じで出てくる。

現時点では動いたり動かなかったりと、安定していません。

2021/12,江端さんの技術メモ

Posted by ebata