如何使用 git bash 中的命令行 Visual Studio 编译器?

How can I use the command-line Visual Studio compiler from git bash?

问题相关。

我想 运行 cl.exe 从 git bash 到 windows。通常你需要开发人员提示来执行此操作,所以我改为这样做:

cmd //V //K "C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\Tools\VsDevCmd.bat"

作为子 shell 打开开发者提示。如果我愿意,我可以从那里 运行 cl

但是,如果我可以从 git bash 运行 命令而不进入子 shell,那就太好了。我会很高兴:

  1. 以某种方式“sourceing”VsDevCmd.bat 文件以导出它在环境中设置为变量的变量,因此我可以 运行 cl 来自 bash,或

  2. //K 更改为 //C 并更改为当前 (bash) 工作目录,然后使用转发的任何参数执行 cl .如果是别名则加分。

根本无法理解#1。

我需要有关 #2 语法的帮助,因为

cmd //V //C "C:\Program Files (x86)\...\VsDevCmd.bat"

在执行批处理脚本并立即退出的意义上有效,我不知道如何将多个命令链接在一起。 运行

cmd //V //C "C:\Program Files (x86)\...\VsDevCmd.bat & cl"

例如,给出

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

如何直接从 git bash 执行 cl.exe,而不需要单独的提示?

我认为没有简单的方法可以做到这一点。我通过使用 cmake 来处理我的项目并只是做

来回避问题
mkdir build
cd build
cmake -G "Visual Studio 16 2019" ..
cmake --build .

也许比我聪明的人可以看看 CMake 的源代码并弄清楚 --build 标志是如何工作的。

对于使用新 Windows 终端在这里徘徊的任何人,这里是我的配置文件,用于 git bash 和 visual studio 编译器:

    {
        "colorScheme": "One Half Dark",
        "commandline": "\"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvars64.bat\" && \"C:\Program Files\Git\bin\bash.exe\" --login",
        "cursorShape": "bar",
        "font": 
        {
            "face": "Lucida Sans Typewriter"
        },
        "guid": "{14ad203f-52cc-4110-90d6-d96e0f41b64d}",
        "icon": "%PROGRAMFILES%/Git/mingw64/share/git/git-for-windows.ico",
        "name": "Git Bash",
        "tabTitle": "Git Bash"
    }