How to write CMakeLists.txt
to conditionally link to the system-wide librt
library only when on Linux environment?
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With