When compiling cross-platform C/C++ projects (like Yamagi Quake II) on Windows using MSYS2, developers often run into a confusing issue: running make inside the MSYS2 MinGW32/MinGW64 shell compiles cleanly, but invoking make from the standard Windows Command Prompt, PowerShell, or an IDE like Code::Blocks triggers a cascade of POSIX compilation errors (such as missing realpath, getuid, setenv, or fcntl).

If your build attempts to compile Unix backends (e.g., src/backends/unix/main.c) instead of Windows backends on a Windows machine, you are likely invoking the wrong subsystem binary. Here is a comprehensive guide to understanding why this happens and how to configure your environment correctly.

The Root Cause: MSYS2 POSIX Layer vs. Native MinGW

MSYS2 is divided into distinct environments:

  • MSYS Environment (/usr/bin): A Cygwin-derived POSIX compatibility layer. The binaries here (like C:\msys64\usr\bin\make.exe or /usr/bin/gcc) emulate a Unix environment. When your Makefile inspects the host OS using tools from /usr/bin, it detects a Unix-like system and builds POSIX targets.
  • MinGW Environments (/mingw32/bin, /mingw64/bin, /ucrt64/bin): Native Windows target toolchains. Binaries compiled here produce native Windows executables and pass native Win32 platform definitions (like _WIN32 and __MINGW32__) to the preprocessor.

When you call make from standard Windows CMD or Code::Blocks using C:\msys64\usr\bin\make.exe, the Makefile detects the POSIX shell environment from /usr/bin instead of the native Windows toolchain, causing it to build src/backends/unix/main.c.

Solution 1: Install and Use the Native MinGW Make Tool

Rather than using the generic POSIX make from /usr/bin, you should install and use the native MinGW version of GNU Make (mingw32-make).

Open your MSYS2 shell and run one of the following commands depending on your target architecture:

# For 32-bit MinGW:
pacman -S mingw-w64-i686-make mingw-w64-i686-gcc

# For 64-bit MinGW (UCRT64 - Recommended):
pacman -S mingw-w64-ucrt-x86_64-make mingw-w64-ucrt-x86_64-gcc

# For standard 64-bit MinGW:
pacman -S mingw-w64-x86_64-make mingw-w64-x86_64-gcc

This will place mingw32-make.exe inside your respective MinGW bin directory (e.g., C:\msys64\mingw32\bin\mingw32-make.exe).

Solution 2: Configure Code::Blocks Toolchain

To use MSYS2 MinGW inside Code::Blocks without environment mismatch errors:

  1. Open Code::Blocks and navigate to Settings > Compiler....
  2. Under Selected compiler, choose GNU GCC Compiler (or create a copy).
  3. Switch to the Toolchain executables tab.
  4. Set the Compiler's installation directory to your MinGW directory, for example:
    C:\msys64\mingw32 (or C:\msys64\ucrt64).
  5. Update the individual program fields to match your MinGW binaries:
    • C compiler: gcc.exe
    • C++ compiler: g++.exe
    • Linker for dynamic libs: g++.exe
    • Linker for static libs: gcc-ar.exe
    • Make program: mingw32-make.exe (Do not point this to C:\msys64\usr\bin\make.exe)
  6. Click OK to save the changes.

Solution 3: Running Make from Windows Command Prompt (CMD/PowerShell)

If you want to invoke builds directly from the Windows Terminal or standard Command Prompt, you must configure your Windows PATH so the MinGW toolchain takes precedence over /usr/bin.

Add the following directory to your system or user PATH environment variable:

C:\msys64\mingw32\bin

Ensure that C:\msys64\usr\bin is either removed from your system PATH or placed lower in priority than the MinGW directory.

Once updated, reopen your terminal and run:

# Verify the compiler
gcc -v

# Build using mingw32-make
mingw32-make

Summary

POSIX compilation errors on Windows usually stem from inadvertently mixing MSYS's /usr/bin POSIX layer with native build targets. By installing mingw-w64-*-make and pointing both your Windows PATH and Code::Blocks to mingw32-make.exe in the MinGW bin folder, Makefiles will correctly identify the platform as Windows and compile the native backends.