Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 333
2NinerRomeo Avatar asked Mar 16 '12 17:03

2NinerRomeo


People also ask

Is CMake only for Linux?

CMake can generate project files for several popular IDEs, such as Microsoft Visual Studio, Xcode, and Eclipse CDT. It can also produce build scripts for MSBuild or NMake on Windows; Unix Make on Unix-like platforms such as Linux, macOS, and Cygwin; and Ninja on both Windows and Unix-like platforms.

What is CMake_ system_ name?

Name of the OS CMake is building for. This is the name of the operating system on which CMake is targeting. On systems that have the uname command, this variable is set to the output of uname -s. Linux, Windows, and Darwin for Mac OS X are the values found on the big three operating systems.

Is CMake platform independent?

CMake is a cross-platform build system generator. Projects specify their build process with platform-independent CMake listfiles included in each directory of a source tree with the name CMakeLists. txt. Users build a project by using CMake to generate a build system for a native tool on their platform.


2 Answers

Inside the CMakeLists.txt file you can do:

IF (WIN32)   # set stuff for windows ELSE()   # set stuff for other systems ENDIF() 
like image 66
karlphillip Avatar answered Sep 29 '22 05:09

karlphillip


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}) 
like image 39
KneLL Avatar answered Sep 29 '22 05:09

KneLL