MacでVSCodeを使ってC言語のデバッグする方法


はじめに

Macでのみ動作確認済みです。
コンパイルして、デバッグが終了したときに、作成したファイルを消去するように設定しています。

ファイルを作成する

mkdir .vscode && \
touch .vscode/tasks.json && \
touch .vscode/launch.json

拡張機能 CodeLLDBを入れる

CodeLLDB - Visual Studio Marketplace
https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb

ファイルの編集

tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "c_build",
            "command": "gcc",
            "args": [
                "-g",
                "${file}",
            ],
            "group": "build", 
            "presentation": {
                "reveal": "never"
            }
        },
        {
            "type": "shell",
            "label": "rm_file",
            "command": "rm",
            "args": [
                "-rf",
                "a.out",
                "a.out.dSYM",
            ],
            "group": "build",
            "presentation": {
                "reveal": "never"
            }

        }
    ]
}
launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "c_debug",
            "type": "lldb",
            "request": "launch",
            "program": "${fileDirname}/a.out",
            "args": [""],
            "cwd": "${fileDirname}",
            "preLaunchTask": "c_build",
            "postDebugTask": "rm_file"
        }
    ]
}

コンパイル方法

  • ⌘ + Shift + b
  • F5

デバッグ

ブレークポイントを設定してから
メニューの「デバッグ」から「デバッグの開始」でデバッグがスタートします。

参考

tasks.jsonの設定について詳しく
Tasks in Visual Studio Code

launch.jsonの設定について詳しく
Debugging in Visual Studio Code