LoginSignup
15
11

More than 3 years have passed since last update.

競プロ C++ VScodeでデバッグできないエラーを解消する(Mac)

Posted at

はじめに

今年の8月に競プロを始めたジョナと申します。

Visual Studio Codeで環境構築をしていたのですが、Mac OS Catalinaでデバッグができないエラーに遭遇したので、初心者なりに解決法を記したいと思います。
今回はデバッグのエラーについてのみ触れるので、詳しい環境構築の情報は下記サイトや他の執筆者様のQiita記事をご参照ください。

[Get Started with C++ and Clang/LLVM in Visual Studio Code](公式サイト 英語)
https://code.visualstudio.com/docs/cpp/config-clang-mac

[macのVSCodeでc++をデバッグする (AtCoderの解答をデバッグする事例つき)]https://qiita.com/tom_sapiens/items/c4c4b7de2201272a94db

デバッグできない問題が発生

公式サイトや記事を参考に、
-tasks.json
-c_cpp_properties.json
-launch.json
を以下のように設定したのですが、ビルドと実行はできてもデバッグができずにブレークポイントで止まらないというエラーが発生しました。

tasks.json
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Build with Clang",
      "type": "shell",
      "command": "g++",
      "args": [
        "-std=c++14",
        "-g",
        "main.cpp",
        "-o",
        "main.out",
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}
c_cpp_properties.json

{
    "configurations": [
      {
        "name": "macOS",
        "includePath": ["${workspaceFolder}/**"],
        "defines": [],
        "macFrameworkPath": [
                          "/System/Library/Frameworks",
                          "/Library/Frameworks"],
        "compilerPath": "/usr/bin/g++",
        "cStandard": "c11",
        "cppStandard": "c++14",
        "intelliSenseMode": "${default}"
      }
    ],
    "version": 4
  }
launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(lldb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/main.out",
      "args": [],
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "lldb",
      "logging": {
        "trace": true,
        "traceResponse": true,
        "engineLogging": true
      }
    }
  ]
}

解決策 CodeLLDBを使う

私はこの問題に何週間も悩まされ続けたのですが、ついに解決法を見つけました。

このスレッドによると、Mac OS CatalinaをインストールしたことによってVSCodeでデバッグできなくなっている人が多くいるようです。

解決策は、VSCodeのExtensionであるCodeLLDBをインストールし、launch.jsonを以下のように書き換えることです。

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "lldb Debug", //nameは何でも大丈夫です
      "type": "lldb", //typeをlldbにすることでCodeLLDBが使えるそうです
      "request": "launch",
      "program": "${workspaceFolder}/main.out",
      "args": [],
    }
  ]
}

結果

CodeLLDBを使うことによって、デバッグができ、ターミナルから入力も受け取れるようになりました!
launch.jsonの書く量も減ってとても楽になりましたね。

Screen Shot 2019-12-17 at 22.03.20.png

もし質問等ありましたら、コメントでお知らせいただけると幸いです。
初心者なりに、わかることがあればお返事させていただきます。
最後まで読んでくださりありがとうございました。

15
11
0

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
15
11