Search code examples
bashbatch-filecmdgit-bash

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


Relate to this question.

I want to run cl.exe from git bash on windows. Usually you need the developer prompt to do this, so I do this instead:

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

Which opens the developer prompt as a sub-shell. From there I can run cl if I want.

However, it would be nice if I could run commands from git bash without going into the subshell. I would be happy with either:

  1. Somehow "sourceing" the VsDevCmd.bat file to export the variables it sets as variables in the environment, so I can run cl from bash, or

  2. changing the //K to a //C and changing to the current (bash) working directory, then executing cl with any arguments forwarded. Bonus points if it's an alias.

Can't figure out #1 at all.

I need help with the syntax on #2 because while

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

works in the sense that it executes the batch script and immediately exits, I don't know how to chain several commands together. Running

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

for instance, gives

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

How can I execute cl.exe from git bash directly, without needing in a separate prompt?


Solution

  • I don’t think there’s a simple way to do this. I skirted the problem by using cmake to handle my project and just doing

    mkdir build
    cd build
    cmake -G "Visual Studio 16 2019" ..
    cmake --build .
    

    Maybe someone smarter than me can look at CMake’s source code and figure out how the --build flag works.