2

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?

Nico Nekoru
  • 2,840
  • 2
  • 17
  • 38
MHebes
  • 2,290
  • 1
  • 16
  • 29
  • Your best bet is to launch bash from a windows console that has already executed vsdevcmd.bat. – jwdonahue Jun 03 '20 at 17:20
  • 1
    Another option is to just run `set > environment.txt` to save the VSDev environment to a file, then write yourself a bash script to process that text and merge it with your bash environment. – jwdonahue Jun 03 '20 at 17:30
  • 1
    I think that you are going to need to setup your environment by hand after seeing what you need. No big deal! All of the paths and allowed chars are different. you don't NEED vsdevcmd.bat.. it isn't magic.. you just need *some* (not all) of the variables it exports. I just did this with success `export PATH=$PATH:/c/Program\ Files\ \(x86\)/Microsoft\ Visual\ Studio/2019/Professional/VC/Tools/MSVC/14.24.28314/bin/Hostx86/x86` and I can access cl.exe (specifically).. Adding your lib paths and the other stuff it needs should not take long. – Señor CMasMas Jun 03 '20 at 19:01
  • Use quotes twice inside like `cmd //V //C """C:\Program Files (x86)\...\VsDevCmd.bat" & cl""` – Nico Nekoru Jun 03 '20 at 22:23
  • @jwdonahue Thanks for that trick, I ended up basically doing that but just fixing it up mostly manually. – MHebes Jun 04 '20 at 01:31
  • @NekoMusume That does not work, bash tries to interpret `cl` itself rather than pass the whole thing to cmd. – MHebes Jun 04 '20 at 01:34
  • @MHebes, just remember that when you update SDK's and the like, you'll have to adjust your bash script environment accordingly. – jwdonahue Jun 04 '20 at 03:33

2 Answers2

5

For anyone wandering here using the new Windows terminal, here is my configuration file to use git bash with visual studio compiler:

    {
        "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"
    }
Traan
  • 134
  • 1
  • 4
0

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.

MHebes
  • 2,290
  • 1
  • 16
  • 29