【问题标题】:How to get Visual Studio to execute build commands in a msys2 shell?如何让 Visual Studio 在 msys2 shell 中执行构建命令?
【发布时间】:2020-05-01 18:36:01
【问题描述】:

我设法使用 Visual Studio Code 构建和调试了我的 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 仍然缺少几个“必备”的 C++ 开发功能。

但我的问题是我不明白如何让 Visual Studio 像 VS Code 那样在不同的 shell 中执行构建命令。所以构建命令不起作用,因为它们不是在 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 像 VS Code 一样在 msys2 shell 中执行构建命令?

编辑

所以也许我越来越接近这个,也许不是。按照 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 文件中定义的“配置”对我来说是个谜……

【问题讨论】:

    标签: mingw visual-studio-2019 msys2


    【解决方案1】:

    这是一种方法:

    // .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"
          }
        }
      ]
    }
    
    

    【讨论】:

      猜你喜欢
      • 2012-07-06
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多