Blog Series Tags

CMake Platform Specific Actions

Sometimes you need to link one library on Windows and another on Linux (or vice versa). CMake has a number of variables that allow you to do this easily:

UNIX TRUE on all UNIX-like OS’s including Apple OS X and CygWin
WIN32 TRUE on Windows including CygWin
APPLE TRUE on Apple systems. **
MINGW TRUE when using the MinGW compiler in Windows
MSYS TRUE when using the MSYS developer environment in Windows
CYGWIN TRUE on Windows when using the CygWin version of cmake

** Note this does not imply the system is Mac OS X only that APPLE is #defined in C/C++ header files. Obtain more specific system information via CMAKE_SYSTEM_VERSION. I.e. IF(${CMAKE_SYSTEM_NAME} MATCHES “Darwin”) then it’s Mac OS X

CMake sets these variables for you. You can use them in your CMake scripts to do specific functions only under a specific Operating System.

IF (WIN32)
    message(STATUS "Microsoft Windows. Building Win32SupportLibrary.")
    add_subdirectory(Win32SupportLibrary)
ELSEIF (UNIX AND NOT MINGW) 
    message(STATUS "Building on Linux. Expecting support to be installed.")
ENDIF()

That’s it! Now you have platform specific build functions.

This is a post in the CMake series.