Use the Microsoft C++ toolset from Bash!
This script load MSVC environment variables using vcvarsall.bat and exports
them.
It is intended to be evaluated (by Bash or any other POSIX-compatible shell,
since it writes a list of export commands to stdout).
Usage: eval "$(vcvarsall.sh [vcvarsall.bat arguments])"
See Microsoft vcvarsall.bat syntax for vcvarsall.bat arguments documentation.
Example (CMake with NMake, recommended):
eval "$(./vcvarsall.sh x64)"
cmake -S . -B build -G "NMake Makefiles"
cmake --build build --config ReleaseExample (CMake with Ninja):
You should explicitly set the C and C++ compilers to cl to ensure MSVC is
used.
eval "$(./vcvarsall.sh x64)"
cmake -S . -B build -G Ninja -DCMAKE_C_COMPILER=cl -DCMAKE_CXX_COMPILER=cl
cmake --build build --config ReleaseExample (using cl directly):
eval "$(./vcvarsall.sh x64)"
cl /nologo /EHsc /Fe:hello.exe hello.cppAnd for other build systems, the logic is always the same:
- First evaluate
vcvarsall.shto set up the environment.eval "$(./vcvarsall.sh x64)"
- Then run your build commands, which will now have access to the MSVC toolset.
Important: You may need to specifyclas the compiler in your build configuration to ensure MSVC is used.
Like vcvarsall.sh, but runs a command instead of exporting variables.
Usage: vcvarsrun.sh [vcvarsall.bat arguments] -- command [arguments...]
Example:
./vcvarsrun.sh x64 -- cl /nologo /EHsc /Fe:hello.exe hello.cpp
./vcvarsrun.sh x64 -- ./my_build_script.shThese scripts are designed to run on Windows with Bash. They work with:
- Git Bash
- WSL
- MSYS2
- Cygwin
To work, these scripts must locate vcvarsall.bat.
By default, it uses vswhere.exe (installed with Visual Studio) to
automatically find the latest Visual Studio installation.
You can override this behavior by setting the VSINSTALLDIR environment
variable (e.g.
VSINSTALLDIR='C:\Program Files\Microsoft Visual Studio\2022\Community').