C++ – Platform detection in CMake


I've 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. I'd like to have CMake detect when I am building on Windows and make the appropriate definitions or command line arguments.

Best Solution

Inside the CMakeLists.txt file you can do:

IF (WIN32)
  # set stuff for windows
  # set stuff for other systems