如何让 Visual Studio 在 msys2 shell 中执行构建命令?

How to get Visual Studio to execute build commands in a msys2 shell?

我设法用 Visual Studio 代码构建和调试我的 C++ (mingw) 项目。我希望能够在 Visual Studio 2019 年完成(如讨论的 here)。为此,我似乎需要 运行 不同 shell 中的构建命令。这是我用于 VS Code 的配置:

.vscode/settings.json 中:

{
    "terminal.integrated.shell.windows": "C:\tools\msys64\usr\bin\bash.exe",
    "terminal.integrated.shellArgs.windows": [
        "--login",
        "-i"
    ],
    "terminal.integrated.env.windows": {
        "MSYSTEM": "MINGW64",
        "CHERE_INVOKING":"1"
    }
}

.vscode/tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "build",
            "command": "sh",
            "args": [
                "build.sh"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

这真的很好用。我的 build.sh 脚本执行各种命令来编译我的项目。

但我希望能够在 Visual Studio 2019 年开发我的项目(而不是 Visual Studio Code),因为 VS Code 仍然缺少一些 "must-have" C++ 开发功能.

但我的问题是我不明白如何让 Visual Studio 在不同的 shell 中执行构建命令 就像 VS Code .所以构建命令不起作用,因为它们不是在 msys2、mingw64 环境中执行的。

这是我的 .vs/tasks.vs.json 文件:

{
  "version": "0.2.1",
  "tasks": [
    {
      "taskLabel": "build",
      "appliesTo": "/",
      "type": "launch",
      "command": "sh",
      "args": ["build.sh"],
      "env": "Mingw64",
      "customLaunchCommand": "C:\tools\msys64\usr\bin\bash.exe",
      "customLaunchCommandArgs": [
        "--login",
        "-i"
      ]
    }
  ]
}

这个配置文件是我关于如何使 sh build.sh 命令在使用命令 C:\tools\msys64\usr\bin\bash.exe --login -i 打开的 shell 中执行的最佳选择。

有人能告诉我如何让 Visual Studio 2019 在 msys2 shell 中执行构建命令,就像 VS Code 一样吗?

编辑:

所以也许我越来越接近这个,也许不是。按照 Visual Studio 2019 文档中的示例,我的 .vs/CppProperties.json 现在看起来像这样:

{
  "configurations": [
    {
      "inheritEnvironments": [
        "mingw_64"
      ],
      "name": "Mingw64",
      "includePath": [
        "${workspaceRoot}\**"
      ],
      "intelliSenseMode": "linux-gcc-x64",
      "environments": [
        {
          "MINGW_PREFIX": "C:\tools\msys64",
          "MINGW_CHOST ": "x86_64-w64-mingw32",
          "MINGW_PACKAGE_PREFIX": "mingw-w64-x86_64",
          "MSYSTEM": "MINGW64",
          "MSYSTEM_CARCH": "x64_64",
          "MSYSTEM_PREFIX": "${env.MINGW_PREFIX}",
          "SHELL": "${env.MINGW_PREFIX}\usr\bin\bash.exe --login -i",
          "TEMP": "${env.MINGW_PREFIX}/../tmp",
          "TMP": "${env.TEMP}"
        }
      ]
    }
  ]
}

还有我的 .vs/tasks.vs.json:

{
  "version": "0.2.1",
  "tasks": [
    {
      "taskLabel": "build",
      "appliesTo": "build.sh",
      "contextType": "build",
      "type": "default",
      "command": "sh",
      "args": [
        "build.sh"
      ],
      "inheritEnvironments": [ "Mingw64" ]
    }
  ]
}

但是,看来Visual Studio 仍然没有使用msys2 shell 来执行构建任务。如何实际使用 CppProperties.json 文件中定义的 "configuration" 对我来说是个谜...

这是一种方法:

// .vs/tasks.vs.json
{
  "version": "0.2.1",
  "tasks": [
    {
      "taskLabel": "build",
      "appliesTo": "build.sh",
      "contextType": "build",
      "type": "launch",
      "command": "bash.exe",
      "args": [
        "--login",
        "-c",
        "\"sh build.sh\""
      ],
      "env": {
        "PATH": "C:\tools\msys64\usr\bin",
        "CHERE_INVOKING": "1",
        "MSYSTEM": "MINGW64"
      }
    }
  ]
}