茂加部珈琲店

主にtech関連のメモ置き場です

VSCodeでcmakeを走らせる

コマンド一つでcmakeやctestを走らせたかったのです。

VSCodeではtask.jsonを使って簡単にできるみたいです.
WindowsではデフォルトのコンパイラVC++なので、MinGWを指定してあります
mingw-64でも動きました。

Ctrl+Shift+B で走ります

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "presentation": {
    "echo": true,
    "reveal": "always",
    "focus": false,
    "panel": "new"
  },
  "tasks": [
    {
      "label": "build",
      "type": "shell",
      "windows": {
        "command": "",
        "args": [
          "mkdir build; cd build; cmake -G 'MinGW Makefiles' ..; cmake --build .; ctest -V -C Debug"
        ]
       },
      "linux": {
        "command": "sh",
        "args": [
          "mkdir build; cd build; cmake ..; cmake --build .; ctest -V"
        ]
       },
       "problemMatcher": []
    }
  ]
}

追記

デフォルトのターミナル設定を書き換えている場合はそっちが実行されるのでexecutableから指定する必要があります

私はMSYS2を使っているので、下記のようにします

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "presentation": {
    "echo": true,
    "reveal": "always",
    "focus": true,
    "panel": "shared",
    "showReuseMessage": false,
    "clear": false
  },
  "windows": {
    "options": {
      "shell": {
        "executable": "cmd.exe",
        "args": ["/d", "/c", "C:\\msys64\\msys2_shell.cmd -mingw64 -defterm -no-start -here -full-path"]
      }
    }
  },
  "tasks": [
    {
      "label": "build",
      "type": "shell",
      "command": "-c",
      "args": [
        "mkdir -p build_msys_gcc; cd build_msys_gcc; cmake --build .; echo; echo '> Finished build task'"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}