Blog Series Tags

CMake Trickery - Platform Specific Linking

Quite often when your working on cross platform code you need to build applications that use one set of libraries when compiled under Microsoft Windows and another set when compiled under Unix/Linux. Fortunately CMake is a cunning tool to help you on your way. It has a bunch of predefined constants that you can use to switch libraries based on the platform like so:

# SomeCoolLibrary
IF (WIN32)
   message(STATUS "Building on Microsoft Windows.")
   set (SomeCoolLibrary someCoolWindows.lib)
   message(STATUS "Building on Linux/Unix.")
   set (SomeCoolLibrary

Notice we check for the enviorenment being Unix but not MinGW. This is because CMake reports the enviorenment as Unix even if it’s MinGW running on Windows (which I suppose makes sense), but it pays to be specific. Finally you can target the generic link library that you’ve mentioned here:

target_link_libraries(MyExecutable ${Boost_LIBRARIES} ${SomeCoolLibrary})

This is a post in the CMake series.