Skip to content

Instantly share code, notes, and snippets.

@rdipardo
Last active August 1, 2023 20:56
Show Gist options
  • Save rdipardo/6a8a84079dbfc01120719a7800236926 to your computer and use it in GitHub Desktop.
Save rdipardo/6a8a84079dbfc01120719a7800236926 to your computer and use it in GitHub Desktop.
Generating MinGW Makefiles on Windows with CMake
# USAGE
# =====
# > mkdir %USERPROFILE%\toolchains
# > copy mingw-win-host.cmake %USERPROFILE%\toolchains
# > mkdir build && cd build
# > cmake .. -G"MinGW Makefiles" -DCMAKE_TOOLCHAIN_FILE=%USERPROFILE%\toolchains\mingw-win-host.cmake
#
SET(CMAKE_SYSTEM_NAME Windows)
# !!!
# compiler toolchain must be on your PATH
# !!!
#
# OPTION 1
# --------
# - go to Control Panel > User Accounts > User Accounts
# - click "Change my environment variables"
# - select PATH and click "Edit"
# - click "New"
# - fill in the box with C:\full\path\to\MinGW64\bin
# - click "OK"
#
# OPTION 2
# --------
# - before running `cmake ..`:
# > set "PATH=C:\full\path\to\MinGW64\bin;%PATH%"
#
SET(CMAKE_MAKE_PROGRAM "mingw32-make.exe")
# which build programs to use, e.g. C:\full\path\to\MinGW64\bin\gcc.exe
find_program(CMAKE_C_COMPILER NAMES gcc)
find_program(CMAKE_CXX_COMPILER NAMES g++)
find_program(CMAKE_AR NAMES ar)
find_program(CMAKE_RANLIB NAMES ranlib)
# where this file is located on disk (assuming %USERPROFILE%\toolchains)
SET(USER_ROOT_PATH ENV{USERPROFILE}/toolchains)
# change "ENV{LOCALAPPDATA}/Programs/Dev-Cpp/MinGW64" to the actual path to your MinGW toolchain
SET(CMAKE_FIND_ROOT_PATH ENV{LOCALAPPDATA}/Programs/Dev-Cpp/MinGW64 ${USER_ROOT_PATH})
# adjust the default behaviour of the FIND_XXX() commands:
# - search for build programs in the host environment?
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# - search for static libraries?
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
# - search for headers?
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment