1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VSCode: C++をclang++でデバッグして配列の中身を見る(macOS)

Last updated at Posted at 2021-10-17

こんにちは、競プロ初心者です。AtCoderを始める際、gccでコンパイルするように設定すると思うのですが、残念ながらgccではVSCodeのデバッグでは(少なくとも私の環境では)配列の中身を見ることができません。そこでclang++を使ってデバッグするようにしようと思ったのですが、そこそこ詰まったのでメモとしてここに書き留めておこうと思います。

2021/10/27
macOS Montereyを使っている場合に出るエラーの解消法を追記しました。

動作環境

MacBook Air (M1, 2020)
macOS Big Sur 11.6
Visual Studio Code 1.61.1

前提

  • Command line tools for Xcodeをインストールしている
  • VSCodeをインストールしている
  • VSCodeの拡張機能C/C++をインストールしている

手順

  1. 拡張機能からCodeLLDBをインストールする
  2. ワーキングディレクトリ中の.vscode/launch.jsonを書き換える
  3. .vscode/task.jsonを書き換える

おまけ #include <bits/stdc++.h> を使えるようにする

拡張機能からCodeLLDBをインストールする

拡張機能のmarketplaceからCodeLLDBをインストールします。

2021/10/27 追記
macOS Montereyを使っていてデバッグ時にエラーが出る場合は ~/.vscode/extentions/vadimcn.vscode-lldb-1.6.8/lldb/bin にあるdebugserverを削除すれば正常に動くようになります。
スクリーンショット 2021-10-17 15.41.27.png

ワーキングディレクトリ中の.vscode/launch.jsonを書き換える

何かしらのcppファイルを開いた状態で「実行とデバッグ」を行い、C++ (GDB/LLDB) → clang++ (usr/bin/clang++) の順に選択すると、ワーキングディレクトリ中の.vscodeというディレクトリの中にlaunch.jsonとtasks.jsonが生成されます。launch.jsonを開き、configurations中のtypeを"lldb"に書き換えます。

launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "clang++ - アクティブ ファイルのビルドとデバッグ",
      "type": "lldb",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb",
      "preLaunchTask": "C/C++: clang++ アクティブなファイルのビルド"
    }
  ]
}

.vscode/task.jsonを書き換える

次に、同じく生成されたtasks.jsonを開き、tasks中のcommandを"/usr/bin/clang++"に書き換え、argsの一番上に"-std=c++20",を追加します。

tasks.json
{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: clang++ アクティブなファイルのビルド",
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++20",
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "デバッガーによって生成されたタスク。"
    }
  ],
  "version": "2.0.0"
}

おまけ #include <bits/stdc++.h> を使えるようにする

ターミナルで以下のコマンドを順に実行します。wgetコマンドが見つからない場合は、homebrewからwgetをインストールしてから再び実行すれば大丈夫です。

$ cd /Library/Developer/CommandLineTools/usr/include/
$ mkdir bits
$ cd bits
$ sudo wget https://gist.githubusercontent.com/reza-ryte-club/97c39f35dab0c45a5d924dd9e50c445f/raw/47ecad34033f986b0972cdbf4636e22f838a1313/stdc++.h

これで配列の中身を見ることができるようになりました。
スクリーンショット 2021-10-17 16.59.52.png

参考文献

  1. AtCoder用C++開発環境 (Mac編)
  2. Visual Studio Code を使った C++ のビルド&デバッグ方法
1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?