vscodeでcygwinを使ってC++デバッグする環境構築(windows)


公式
https://www.cygwin.com/
https://code.visualstudio.com/docs/cpp/config-mingw
https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools

目的
vscodeでcygwinを用いてc++をデバッグ

方針
①cygwinをダウンロード
②vscodeの拡張機能インストール
③tasks.json設定
④launch.json設定
⑤helloworldをデバッグ実行


①cygwinをダウンロード

https://www.cygwin.com/
からcygwinをダウンロードしてexe実行

とりあえずまずは次へ連打でインストール進める。
配置場所はc直下想定。

一度インストール終わったら再度exe起動して
g++
gdb
をリストから選択して、最新版をインストール。

完了したらc/cygwin/binにPATHを通す。

cygwin64とか32とか名前うろ覚えだから自分の環境を調べる必要あり。


②vscodeの拡張機能インストール

とりあえずhttps://code.visualstudio.com/docs/cpp/config-mingw
にそって、プロジェクトフォルダ作ってvscode起動

cmd
mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .

https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools
から拡張機能インストール

Ctrlキー+ Shiftキー+ Pで、コマンドパレットを開く。
「C / C ++」と入力してから、候補のリストから「構成の編集(UI)」を選択。

これにより、C / C ++ IntelliSense構成ページが開く。

ここで変更を行うと、VS Codeはc_cpp_properties.json.vscodeフォルダーで呼び出されるファイルにそれらを書き込む。

コンパイラのパス設定、と書かれた項目を見つける。
cygwinまでのパス\bin\g++.exe
に変更。

IntelliSenseモードをgcc-x64に設定。

.vscode/c_cpp_properties.jsonを開いて、

c_cpp_properties.json
"compilerPath": "cygwinのbinまでのパス\\g++.exe"

になっていることを確認。


③tasks.json設定
メインメニューから、[表示]> [コマンドパレット ]を選択し、「task」と入力して[タスク:デフォルトビルドタスクの設定]を選択。

ドロップダウンで、テンプレートから「tasks.jsonファイルを作成」を選択し、「その他」を選択。

VS Codeは最小限のtasks.jsonファイルを作成し、エディターで開く。

先に進み、ファイルの内容全体を次のコードスニペットで置き換える。

tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build hello world",
      "type": "shell",
      "command": "g++",
      "args": ["-g", "-o", "helloworld", "helloworld.cpp"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

args配列は、g ++に渡されるコマンドライン引数を指定する。

helloworld以外のファイル名をコンパイルする場合は、
各2つのhelloworld部分を拡張子を除いたファイル名に変更する。


④launch.json設定

コマンドパレットから「launch」と入力し、[デバッグ]を選択。:launch.jsonを開きます。次に、GDB / LLDB環境を選択。

launch.json

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/helloworld.exe",
      "args": [],
      "stopAtEntry": true,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "cygwinまでのパス\\bin\\gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

externalConsoleをfalseにしておかないと、
vscodeのコンソール上にprint文がはかれなかった。

externalConsoleは外部コンソールでプログラムを実行するか否かのオプションらしい。

"program": "${workspaceFolder}/helloworld.exe"
は適宜変更する。


⑤helloworldをデバッグ実行

VSコードのメインメニューで、[ ファイル ] > [新規ファイル ]をクリックして、helloworld.cppファイル作成。

helloworld.cpp
#include <iostream>

using namespace std;

int main(){
  cout << "Hello world" << endl;
  return 0;
}

Ctrl + Shift + Bでビルドタスク(tasks.json)を実行。

問題がなければhelloworld.exeが作成される。

F5キー押下でデバッグ実行される。

コードエディターは、
mainメソッドの最初のステートメントを強調表示する。
これは、C ++拡張機能が自動的に設定するブレークポイント(個人で設定可能)。

F5キーをもう一度押下でhelloworldが出力される。



"version"とか変更できそうな部分は公式

を参考に適宜変更してください。