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++プログラムのデバッグを行う

Last updated at Posted at 2021-04-28

概要

vagrant 環境で開発した C++プログラムを VSCode で検証する際に、GDB を使おうと思い、試行錯誤した内容を備忘録として残します。

環境

  • VSCode 1.55.2
  • vagrant + VirtualBox (CentOS)

C++プログラムの設定

プログラムを make する際に、デバッグモードにしておく必要があるようです。

VSCode の設定

VSCode で GDB を実行するためには、.vscodeディレクトリ直下にあるlaunch.jsonファイルを設定する必要があります。

launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "g++ デバッグ",
      "type": "cppdbg",
      "request": "launch",
      "program": "/home/vagrant/(実行ファイルまでパス)/(実行ファイル名)",
      "args": ["aaa.txt", "bbb.log", "ccc.p" "-d"],
      "stopAtEntry": false,
      "cwd": "/home/vagrant/(データのあるパス)",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "gdbの再フォーマットを有効にする",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "C/C++: g++ アクティブなファイルのビルド",
      "miDebuggerPath": "/usr/bin/gdb"
    }
  ]
}

下記のうち、設定が必要な項目は下記の4つです。

  • name : 実行するデバッグの名称。なんでも良い
  • program : 実行ファイルを指定(絶対パスで指定しました)
  • args : プログラム実行の際に必要な引数を指定(必要なファイル名、オプション名を""で囲んで一つずつ指定する)
  • cwd : プログラム実行に必要なファイルのディレクトリを指定

stopAtEntryを true にすれば、デバッグ開始後、処理の冒頭で自動でブレイクします。

注意点

上記設定後にデバッガーを実行したところ、エラーが発生しました。
色々と原因を調べてみたところ、launch.jsonファイルの下記記述が悪さをしていたようです。
"preLaunchTask": "C/C++: g++ アクティブなファイルのビルド",
本記述は、task.jsonファイルの設定で事前にビルドをしてくれるようですが、私の環境ではこの事前ビルドでエラーが発生したため、本記述を削除したところ正常にデバッガーを起動できました。

GDB 実行方法

launch.json ファイル設定後、下記手順を実行すれば、デバッガが起動できました。

  1. VSCode を再起動する(launch.json の変更を反映するため)
  2. VSCode 画面左側の「実行とデバッグ」ボタン(虫のマーク)をクリックする
  3. 画面左上の「実行とデバッグ」のプルダウンから、name で指定したデバッガを選択する
  4. プルダウンのすぐ左の再生ボタンをクリックする
1
1
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
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?