今度こそ動くC++のデバッグ環境

## はじめに

講義で必要なのでC++を実行できるようにしたいが、どのQiitaやサイトを読んでも何故か俺の環境では動かない。俺は世界に嫌われているのか。泣いた。

## やりたいこと

- VSCodeでC++デバッグ
- 変数の中身とかみたい
- 統合ターミナル使いたい。外部ターミナルはいや。
- ビルドファイルとかは残さなくていい。立つ鳥跡を濁さず。

## 環境

コマンドのコピペから読み取ってください

```
% neofetch
                    'c.          shirokuma89dev@shiro89dev.local
                 ,xNMM.          -------------------------------
               .OMMMMo           OS: macOS 14.1 23B74 arm64
               OMMM0,            Host: Mac14,2
     .;loddo:' loolloddol;.      Kernel: 23.1.0
   cKMMMMMMMMMMNWMMMMMMMMMM0:    Uptime: 5 days, 23 hours, 9 mins
 .KMMMMMMMMMMMMMMMMMMMMMMMWd.    Packages: 30 (brew)
 XMMMMMMMMMMMMMMMMMMMMMMMX.      Shell: zsh 5.9
;MMMMMMMMMMMMMMMMMMMMMMMM:       Resolution: 1710x1112
:MMMMMMMMMMMMMMMMMMMMMMMM:       DE: Aqua
.MMMMMMMMMMMMMMMMMMMMMMMMX.      WM: Quartz Compositor
 kMMMMMMMMMMMMMMMMMMMMMMMMWd.    WM Theme: Blue (Dark)
 .XMMMMMMMMMMMMMMMMMMMMMMMMMMk   Terminal: iTerm2
  .XMMMMMMMMMMMMMMMMMMMMMMMMK.   Terminal Font: UDEVGothic35NFLG-Regular 16
    kMMMMMMMMMMMMMMMMMMMMMMd     CPU: Apple M2
     ;KMMMMMMMWXXWMMMMMMMk.      GPU: Apple M2
       .cooc,.    .,coo:.        Memory: 4648MiB / 24576MiB
```

```bash:
% gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc/13.2.0/bin/../libexec/gcc/aarch64-apple-darwin23/13/lto-wrapper
Target: aarch64-apple-darwin23
Configured with: ../configure --prefix=/opt/homebrew/opt/gcc --libdir=/opt/homebrew/opt/gcc/lib/gcc/current --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-13 --with-gmp=/opt/homebrew/opt/gmp --with-mpfr=/opt/homebrew/opt/mpfr --with-mpc=/opt/homebrew/opt/libmpc --with-isl=/opt/homebrew/opt/isl --with-zstd=/opt/homebrew/opt/zstd --with-pkgversion='Homebrew GCC 13.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --with-system-zlib --build=aarch64-apple-darwin23 --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk --with-ld=/Library/Developer/CommandLineTools/usr/bin/ld-classic
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 13.2.0 (Homebrew GCC 13.2.0)
```

```bash:
% g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc/13.2.0/bin/../libexec/gcc/aarch64-apple-darwin23/13/lto-wrapper
Target: aarch64-apple-darwin23
Configured with: ../configure --prefix=/opt/homebrew/opt/gcc --libdir=/opt/homebrew/opt/gcc/lib/gcc/current --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-13 --with-gmp=/opt/homebrew/opt/gmp --with-mpfr=/opt/homebrew/opt/mpfr --with-mpc=/opt/homebrew/opt/libmpc --with-isl=/opt/homebrew/opt/isl --with-zstd=/opt/homebrew/opt/zstd --with-pkgversion='Homebrew GCC 13.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --with-system-zlib --build=aarch64-apple-darwin23 --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk --with-ld=/Library/Developer/CommandLineTools/usr/bin/ld-classic
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 13.2.0 (Homebrew GCC 13.2.0)
```

## 動くもの

動かなかったらごめんなさい。2023/11/06時点

```json:tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build",
            "type": "shell",
            "command": "/usr/bin/g++",
            "args": [
                "-std=c++17",
                "-gdwarf-3",
                "${fileBasename}",
                "-o",
                "${fileBasenameNoExtension}",
                "--debug"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Delete Files",
            "type": "shell",
            "command": "rm",
            "args": [
                "-rf",
                "${fileBasenameNoExtension}.dSYM",
                "${fileBasenameNoExtension}"
            ]
        }
    ]
}
```

```json:launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Run",
            "type": "lldb",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}",
            "args": [],
            "cwd": "${workspaceFolder}",
            "preLaunchTask": "Build",
            "postDebugTask": "Delete Files"
        }
    ]
}
```

## 終わりに

もはや将来の俺用の記事ですがよしとします。

Delete Fileというタスクを作ることで、プログラム終了後にビルドファイルなどが残らないようにしました。学習環境にはこちらの方が向いていると思います。

残したい場合はlaunch.jsonのconfigurationsのpostDebugTaskからDelete Filesを消せばいいです。