Is there a guide on how to configure CMake with MinGW/MSYS and git on Windows?

Hi all,

I am a maintainer of a library (OpenXLSX) and have recently augmented the CMake configuration to auto-pull dependencies via find_package or FetchContent from their source repositories, instead of including a copy of the dependencies with our own library.

I have been asking one of our Windows users to test a new configuration for me, and it seems to be non-trivial to get git working for FetchContent.

On their configuration, CMake complains about not finding git.

As this is a generic “getting started” problem:

Is there a comprehensive guide on installing CMake on Windows so that integration with git is working & commands like FetchContent can be used?

I have no access to a Windows test system, so I can’t trial & error a solution for this.

Any help is appreciated so I can instruct our users on how to prepare their CMake for building OpenXLSX.

Cheers!

Do you mean building under MinGW/MSYS or working under windows with git for windows?

See Using CMake in MSYS2 - MSYS2

With you phrasing this as an “or” question - are you saying that CMake on Windows runs without MinGW?

My question, reduced to the simplest form is:

“How do Windows users install CMake so that it can build projects whose CMake configuration invokes FetchContent commands?”

with the implication being that - as per the observed errors - FetchContent uses git in the background, and CMake in the install that my user reported a test from, does not “find” git.

That depends on what they want todo?

What is your build system and what os target system?

And what is the toolchain you want to use?

The key is to use a consistent MinGW environment, not MSYS or Git Bash.

Install via MSYS2:

  • mingw-w64-x86_64-cmake

  • mingw-w64-x86_64-toolchain

  • git

Then run everything from the MinGW64 shell:

cmake -G Ninja ..
cmake --build .

FetchContent works out of the box as long as Git is available in PATH.

Avoid mixing MSYS, MinGW, and Git Bash—they use different environments and will break detection.

Your last bullet list sounds like what I am looking for, and reasonably to the point.
Regarding your prior comment: I don’t understand the question about the build system - it’s CMake, hence me posting here :slight_smile: The target system for Windows users is the system on which the build is taking place, i.e. also Windows.

For the toolchain: I would assume both work, gcc/g++ or MSVC, but I have no experience with MSVC (nor do I intend to ever touch it), so let’s focus on g++ for starters.

I will see if one of our users can test the installation sequence you proposed:

  • install MSYS2
  • via MSYS2, install
    • mingw-w64-x86_64-cmake
    • mingw-w64-x86_64-toolchain
    • git
  • run cmake from the mingw64 shell (without ninja, which I assume you wrote as an example)

@ClausKlein another suggestion I got from a C++ chat group is to use the windows package manager like so:

winget install -e --id Kitware.CMake
winget install -e --id Git.Git

Should that work as well?

For MSYS you need a cmake built for MSYS as the MSYS eco system uses a special path mapping internally

Duly noted. Is your comment in reference to MSYS2 as well, as suggested by Claus?

Yes, as Claus already noted in his instructions

Okay, so here is the list of steps that I actually needed and for which I needed to do some digging.

It would be really nice if you could include these (with optional build systems MSYS, mingw and Ninja) into the documentation:

Install MSYS2:

In a Powershell (run as Administrator): run winget install --id MSYS2.MSYS2 -e
→ After performing this step, the “MSYS2 MSYS shell” and the “MSYS2 MinGW 64-bit shell” should be accessible from the Start Menu

In MSYS2 MSYS shell: run pacman -Syu
The previous instruction might require a close & reopen of the MSYS2 MSYS shell

In MSYS2 MSYS shell: run pacman -Su

Install development toolchain:
In MSYS2 MinGW 64‑bit shell: run pacman -S --needed base-devel mingw-w64-x86_64-toolchain

Install cmake & git in MSYS2 environment:

In MSYS2 MinGW 64‑bit shell: run pacman -S --needed mingw-w64-x86_64-cmake mingw-w64-x86_64-git
(optional) In MSYS2 MinGW 64‑bit shell: run pacman -S --needed mingw-w64-x86_64-ninja

**
Verify installation:**
In MSYS2 MinGW 64‑bit shell: verify versions of git, cmake, gcc, g++

git --version; cmake --version; gcc --version; g++ --version

Build your project:

cd project/
mkdir build; cd build

Then:

cmake .. -G "MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release
cmake --build . --parallel

or

cmake .. -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release
cmake --build . --parallel

or

cmake .. -G "Ninja" -DCMAKE_BUILD_TYPE=Release
ninja