2NinerRomeo 2NinerRomeo - 1 month ago 21
C++ Question

Platform detection in Cmake

Ive added some functionality from boost::asio, which has precipitated some compiler "warnings":


Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately.


That problem was dealt with here. What I'd like to do is have CMake detect when I am building on windows and make the appropriate definitions or command line arguments.

Answer

Here is a simple solution.

macro(get_WIN32_WINNT version)
    if (WIN32 AND CMAKE_SYSTEM_VERSION)
        set(ver ${CMAKE_SYSTEM_VERSION})
        string(REPLACE "." "" ver ${ver})
        string(REGEX REPLACE "([0-9])" "0\\1" ver ${ver})

        set(${version} "0x${ver}")
    endif()
endmacro()

get_WIN32_WINNT(ver)
add_definitions(-D_WIN32_WINNT=${ver})
Comments