Using CMake on Windows

CMake on Windows is installed via the cmake*-win64-x64.msi graphical installer. Do not use sudo or Run As Administrator for cmake in general.

Compile programs using CMake

  1. Navigate to the directory containing the file CMakeLists.txt using the Windows Terminal / Command Prompt.
  2. configure the build. This is normally run only once unless making major project changes.

    cmake -G "MinGW Makefiles" -DCMAKE_SH="CMAKE_SH-NOTFOUND" -B build .
  3. compile the program. This is the command run each time you make a change to the project code.

    cmake --build .
  4. Optionally, install the program with

    cmake --build . --target install

Generator selection

On Windows, CMake defaults to Visual Studio and Nmake. The cmake options above direct the build to MinGW. If you wish to make this change permanent for CMake ≥ 3.15, set the Windows environment variable CMAKE_GENERATOR to “MinGW Makefiles”. This can still be overridden if needed like

cmake -G "Visual Studio 16 2019"

sh.exe error with cmake

The nuisance error from cmake about sh.exe being on the Path. This error also happens with CMake Windows builds on Azure Pipelines and GitHub Actions.

sh.exe was found in your PATH, here: C:/Program Files/Git/user/bin/sh.exe For MinGW make to work correctly sh.exe must NOT be in your path. Run cmake from a shill that does not have sh.exe in your PATH. If you want to use a UNIX shell, then use MSYS Makefile

Eliminate this message by adding -DCMAKE_SH="CMAKE_SH-NOTFOUND" to the cmake command, like:

cmake -G "MinGW Makefiles" -DCMAKE_SH="CMAKE_SH-NOTFOUND" ..

Notes