Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional CMAKE link to rt-library

Tags:

c++

c

cmake

How to write CMakeLists.txt to conditionally link to the system-wide librt library only when on Linux environment?

like image 377
Cartesius00 Avatar asked Oct 08 '11 08:10

Cartesius00


People also ask

How do I link libraries in CMakeLists?

Let's start by adding the library's directory as a subdirectory to our myapp project. add_subdirectory makes the library test defined in libtestproject available to the build. In target_link_libraries we tell CMake to link it to our executable. CMake will make sure to first build test before linking it to myapp.

What does target link libraries do in CMake?

Specify libraries or flags to use when linking a given target and/or its dependents. Usage requirements from linked library targets will be propagated. Usage requirements of a target's dependencies affect compilation of its own sources.


1 Answers

cmake has several predefined variables useful for environment detection (WIN32, UNIX, APPLE, CYGWIN). Here is the full list: http://www.cmake.org/cmake/help/cmake-2-8-docs.html#section_VariablesThatDescribetheSystem

So you can write something like

if(UNIX AND NOT APPLE)
    target_link_libraries(target_name rt)
endif()
like image 73
Andrey Kamaev Avatar answered Sep 21 '22 04:09

Andrey Kamaev