Compiling for Windows - andrewrk/libsoundio GitHub Wiki

Note: See the Building for Windows with MXE section in README.md. This is an alternate guide for building for Windows written by a user.

Create a file, toolchain.cmake:

# Required by cmake if `uname -s` is inadaquate
set(CMAKE_SYSTEM_NAME               Windows)
set(CMAKE_SYSTEM_VERSION            1)

# Mingw architecture default is 64-bit
# To override: 
#   $ export MINGW_ARCH=32 
if(DEFINED ENV{MINGW_ARCH}) 
        set(MINGW_ARCH              "$ENV{MINGW_ARCH}")
else()
        set(MINGW_ARCH              "64")
endif()

if(${MINGW_ARCH} STREQUAL "32")
        set(CMAKE_SYSTEM_PROCESSOR  "i686")
elseif(${MINGW_ARCH} STREQUAL "64")
        set(CMAKE_SYSTEM_PROCESSOR  "x86_64")
else()
        message(FATAL_ERROR         "Unknown system architecture specified") 
endif()

# Path to mingw
set(MINGW_PREFIX                    "/opt/mingw${MINGW_ARCH}")
# Linux mingw requires explicitly defined tools to prevent clash with native system tools
set(MINGW_TOOL_PREFIX               ${MINGW_PREFIX}/bin/${CMAKE_SYSTEM_PROCESSOR}-w64-mingw32-)

# Windows msys mingw ships with a mostly suitable preconfigured environment
if(DEFINED ENV{MSYSCON})
        set(CMAKE_GENERATOR         "MSYS Makefiles" CACHE STRING "" FORCE)
        set(MINGW_PREFIX            "/mingw${MINGW_ARCH}")
        set(MINGW_TOOL_PREFIX       "${MINGW_PREFIX}/bin/") 

        # Msys compiler does not support @CMakeFiles/Include syntax
        set(CMAKE_C_USE_RESPONSE_FILE_FOR_INCLUDES   OFF)
        set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES OFF)
endif()

# The target environment
set(CMAKE_FIND_ROOT_PATH            ${MINGW_PREFIX})
set(CMAKE_INSTALL_PREFIX            ${MINGW_PREFIX})

# which compilers to use for C and C++
set(CMAKE_C_COMPILER                ${MINGW_TOOL_PREFIX}gcc)
set(CMAKE_CXX_COMPILER              ${MINGW_TOOL_PREFIX}g++)
set(CMAKE_RC_COMPILER               ${MINGW_TOOL_PREFIX}windres)

# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search 
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

# Fix header finds
set(WASAPI_INCLUDE_DIR              ${MINGW_PREFIX}/include)

# Set release to be defaut
if(NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE        "Release" CACHE STRING "Defaulting to Release build type for mingw")
endif()

Linux Prerequisites

Cross-compiling using mingw toolchain on Linux

  1. Fetch mingw64 (... or mingw32, the two can co-exist; adjust as needed.)
sudo apt-get install mingw64-x-gcc mingw64-x-pkgconfig
                    # ^--  or mingw32-x-gcc mingw32-x-pkgconfig, or both

Windows Prerequisites

Compiling directly on windows using the msys2 POSIX style build environment

  1. Fetch msys2 installer
  2. Update msys2
pacman -Sy; pacman --needed -S bash pacman pacman-mirrors msys2-runtime
  1. Important: Restart msys2 and update the rest of your packages, then install mingw64 (or mingw32, the two can co-exist; adjust as needed.)
pacman -Su; pacman -S mingw-w64-x86_64-gcc
                     # ^--  or mingw-w64-i686-gcc, or both
  1. Add required packages, and some useful packages:
pacman -S  make cmake pkgconfig
pacman -S  p7zip gzip tar wget vim
          # ^-- optional, may come in handy :)
  1. Important: Close out msys2 console and fire up the newly installed Mingw64 console launcher.

Cloning Source

  1. Clone the source code, as you'd any GitHub project
git clone -b master https://andrewrk/libsoundio

Build

  1. First, configure the project
cd libsoundio; mkdir build out; cd build;
cmake .. -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake -DCMAKE_INSTALL_PREFIX=../out
  1. Compile
make install
  1. The build will install lib/*.dll files, bin/*.exe as you'd expect on a Linux environment which can be used in a Windows application.

Debug

  1. To create a debug binary, simply define CMAKE_BUILD_TYPE as Debug in your cmake command
make clean; rm CMakeCache.txt # cleanup previous builds
- cmake .. -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake -DCMAKE_INSTALL_PREFIX=../out
+ cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake -DCMAKE_INSTALL_PREFIX=../out