175

I am a beginner to CMAKE. Below is a simple cmake file which works well in mingw environment windows. The problem is clearly with target_link_libraries() function of CMAKE where I am linking libwsock32.a. In windows this works and I get the results.

However, as expected, in Linux, the /usr/bin/ld will look for -lwsock32 which is NOT there on the Linux OS.

My Problem is: How do I instruct CMAKE to avoid linking wsock32 library in Linux OS???

Any help will be greatly appreciated.

My Simple CMake file:

 PROJECT(biourl)
 set (${PROJECT_NAME}_headers ./BioSocketAddress.h  ./BioSocketBase.h ./BioSocketBuffer.h ./BioSocketCommon.h  ./BioSocketListener.h  ./BioSocketPrivate.h  ./BioSocketStream.h ./BioUrl.h BioDatabase.h )

set (${PROJECT_NAME}_sources BioSocketAddress.C  BioSocketBase.C  BioSocketCommon.C BioSocketStream.C  BioUrl.C BioDatabase.C )

add_library(${PROJECT_NAME} STATIC ${${PROJECT_NAME}_headers} ${${PROJECT_NAME}_sources} )

# linkers
#find_library(ws NAMES wsock32 PATHS ${PROJECT_SOURCE_DIR} NO_SYSTEM_ENVIRONMENT_PATH NO_DEFAULT_PATH)

target_link_libraries(${PROJECT_NAME} bioutils wsock32)

install (TARGETS ${PROJECT_NAME}
       RUNTIME DESTINATION bin
       LIBRARY DESTINATION lib
       ARCHIVE DESTINATION lib/archive )

10 Answers 10

223

Use

if (WIN32)
    # do something
endif (WIN32)

or

if (UNIX)
    # do something
endif (UNIX)

or

if (MSVC)
    # do something
endif (MSVC)

or similar

see CMake Useful Variables and CMake Checking Platform

6
120

Given this is such a common issue, geronto-posting:

if(UNIX AND NOT APPLE)
    set(LINUX TRUE)
endif()

# if(NOT LINUX) should work, too, if you need that
if(LINUX) 
    message(STATUS ">>> Linux")
    # linux stuff here
else()
    message(STATUS ">>> Not Linux")
    # stuff that should happen not on Linux 
endif()

CMake boolean logic docs

CMake platform names, etc.

7
  • 14
    Thanks for mentioning APPLE. Jan 28, 2016 at 15:50
  • 1
    @VictorSergienko Всегда рад помочь :)
    – mlvljr
    Jan 28, 2016 at 20:14
  • 5
    dont assume unix is linux. link to the cmake useful variables website for cmake_system_name. use Linux mixed case OS detector
    – don bright
    Oct 22, 2016 at 17:24
  • tibur's answer is better
    – don bright
    Oct 25, 2016 at 1:28
  • 2
    Yeah, FreeBSD will also pass (UNIX AND NOT APPLE) ... and @mlvljr 's link has changed to: gitlab.kitware.com/cmake/community/-/wikis/doc/tutorials/… now.
    – SlySven
    Mar 24, 2020 at 23:12
74

In General

You can detect and specify variables for several operating systems like that:

Detect Microsoft Windows

if(WIN32)
    # for Windows operating system in general
endif()

Or:

if(MSVC OR MSYS OR MINGW)
    # for detecting Windows compilers
endif()

Detect Apple MacOS

if(APPLE)
    # for MacOS X or iOS, watchOS, tvOS (since 3.10.3)
endif()

Detect Unix and Linux

if(UNIX AND NOT APPLE)
    # for Linux, BSD, Solaris, Minix
endif()

Your specific linker issue

To solve your issue with the Windows-specific wsock32 library, just remove it from other systems, like that:

if(WIN32)
    target_link_libraries(${PROJECT_NAME} bioutils wsock32)
else()
    target_link_libraries(${PROJECT_NAME} bioutils)
endif()
6
  • 3
    What does one use for Solaris?
    – jww
    Aug 29, 2017 at 11:16
  • 1
    Typo: MSVS should be MSVC. I tried to edit it for you but stackoverflow doesn't allow edits that are less than 6 characters for some reason...
    – mchiasson
    Mar 11, 2018 at 15:25
  • 2
    According to the documentation, "APPLE" only implies, that we're building for an apple target; i.e. OSX, but also iOS, watchOS etc. Are there any ways to detect os X in a reliable manner?
    – user1596212
    Mar 28, 2018 at 22:19
  • @Julien if you are building for iOS, tvOS or watchOS, you're most likely going to be using a cmake toolchain file, which should have some kind of variable set in there that could be used to achieve what you're looking for.
    – mchiasson
    Apr 7, 2018 at 21:43
  • @Julien FWIW: the cmake documentation only confirms that it also includes iOS, watchOS, tvOS since 3.10.3
    – itMaxence
    Jul 25, 2018 at 11:11
31

You have some special words from CMAKE, take a look:

if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
    // do something for Linux
else
    // do something for other OS
5
  • 4
    The standard CMake way: internally-inconsistent :) [this is one right / to-the-point answer, though]
    – mlvljr
    Aug 27, 2015 at 20:44
  • 3
    For those searching, here is the list of names github.com/Kitware/CMake/blob/master/Modules/…
    – A T
    Apr 15, 2020 at 2:45
  • 3
    STREQUAL accepts variables (in addition to string) as first operand, so it could be the more concise if(CMAKE_SYSTEM_NAME STREQUAL "Linux")...
    – Ad N
    Jul 6, 2020 at 14:37
  • @AdN Are you sure it's only on the first operand ? For example, if(CMAKE_CXX_COMPILER_ID STREQUAL MSVC) will not work because MSVC happens to be a variable. Nov 10, 2021 at 2:17
  • 1
    @JohanBoulé It also accepts a variable for the second operand indeed (cmake.org/cmake/help/latest/command/if.html#comparisons). Although misleading, my comment did not really claim it worked only for first operand though, but for this specific answer it only made sense for the first operand ; )
    – Ad N
    Nov 10, 2021 at 8:37
28

Modern CMake Way

Avoid using WIN32, APPLE, etc. Excerpt of a moderator's reply on official forum:

The WIN32, APPLE, UNIX, etc. variables are “soft” deprecated [...] CMAKE_SYSTEM_NAME is what I’d use in CMake code, PLATFORM_ID is needed in generator expressions.

What possible values can CMAKE_SYSTEM_NAME or PLAFORM_ID take? Refer the source.

How to detect a platform

Use STREQUAL:

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
  # Linux-specific stuff
endif ()

How to detect multiple platforms

Create a list variable and use IN_LIST:

set(OPENGL_PLATFORMS Linux Windows)
if (CMAKE_SYSTEM_NAME IN_LIST OPENGL_PLATFORMS)
  # platform-specific stuff e.g.
  find_package(OpenGL REQUIRED)
endif ()

Generator Expression

Use PLATFORM_ID:

target_link_libraries(TARGET_NAME PRIVATE
  $<$<PLATFORM_ID:Linux,Windows>:OpenGL::GL>)

Aside: Generator expressions can only be used if the manual calls it out. For example target_link_libraries's documentation calls it out while set_target_properties doesn't. I'd to read CMake: set_target_properties fails with target defined by generator expression to realize why.

2
  • 3
    I've no issues with downvoting. Silently downvoting is useful to vent out your emotion, what's more useful is to tell why. It'd help others (and myself) learn from what's wrong with this post. Thanks!
    – legends2k
    Mar 3, 2022 at 7:17
  • 1
    "Modern CMake Way" works as CMake 3.21.0 on my Mac Os, while below solution doesn't. Just to let you know.
    – Kabu
    May 27, 2022 at 7:13
26

Generator expressions are also possible:

target_link_libraries(
    target_name
    PUBLIC
        libA
        $<$<PLATFORM_ID:Windows>:wsock32>
    PRIVATE
        $<$<PLATFORM_ID:Linux>:libB>
        libC
)

This will link libA, wsock32 & libC on Windows and link libA, libB & libC on Linux

CMake Generator Expressions

1
  • 1
    Thanks for this just you add extra ">". which is "$<$<PLATFORM_ID:Windows>:wsock32>"
    – wow2006
    Feb 5, 2019 at 12:49
9

I want to leave this here because I struggled with this when compiling for Android in Windows with the Android SDK.

CMake distinguishes between TARGET and HOST platform.

My TARGET was Android so the variables like CMAKE_SYSTEM_NAME had the value "Android" and the variable WIN32 from the other answer here was not defined. But I wanted to know if my HOST system was Windows because I needed to do a few things differently when compiling on either Windows or Linux or IOs. To do that I used CMAKE_HOST_SYSTEM_NAME which I found is barely known or mentioned anywhere because for most people TARGEt and HOST are the same or they don't care.

Hope this helps someone somewhere...

7

Try that:

if(WIN32)
    set(ADDITIONAL_LIBRARIES wsock32)
else()
    set(ADDITIONAL_LIBRARIES "")
endif()

target_link_libraries(${PROJECT_NAME} bioutils ${ADDITIONAL_LIBRARIES})

You can find other useful variables here.

1
  • This worked and I personally like this since its very intuitive. Thank you very much.
    – Prasad
    Feb 7, 2012 at 3:11
1

simple like

target_link_libraries(${PROJECT_NAME}
  aaa
  bbb
  ccc
  $<$<BOOL:$<PLATFORM_ID:Linux>>:rt>
  $<$<BOOL:$<PLATFORM_ID:Linux>>:dl>
  )
-6

Use some preprocessor macro to check if it's in windows or linux. For example

#ifdef WIN32
LIB= 
#elif __GNUC__
LIB=wsock32
#endif

include -l$(LIB) in you build command.

You can also specify some command line argument to differentiate both.

1
  • 7
    User is asking for CMake makefiles.
    – tibur
    Feb 6, 2012 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.