cmake: detect librt for POSIX time functions

per clock_gettime(3),

> On POSIX systems on which these functions are available, the symbol
> _POSIX_TIMERS is defined in <unistd.h> to a value greater than 0.

and

> Most systems require the program be linked with the librt library to
> use these functions.

so, we should detect this symbol and link against librt for using these
functions.

in this change, librt is linked for checking the existence of
clock_gettime(), if it exists. RT_LIBRARY is defined.

Signed-off-by: Kefu Chai <kchai@redhat.com>
This commit is contained in:
Kefu Chai 2019-11-12 06:55:40 +08:00
parent 7ffb5d9e79
commit d6998a3b95
2 changed files with 9 additions and 2 deletions

View File

@ -67,6 +67,13 @@ CHECK_TYPE_SIZE(__s64 __S64)
unset(CMAKE_EXTRA_INCLUDE_FILES)
include(CheckSymbolExists)
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LIBRARIES rt)
check_symbol_exists(_POSIX_TIMERS "unistd.h;time.h" HAVE_POSIX_TIMERS)
cmake_pop_check_state()
if(HAVE_POSIX_TIMERS)
find_library(RT_LIBRARY NAMES rt)
endif()
check_symbol_exists(res_nquery "resolv.h" HAVE_RES_NQUERY)
check_symbol_exists(F_SETPIPE_SZ "linux/fcntl.h" CEPH_HAVE_SETPIPE_SZ)
check_symbol_exists(__func__ "" HAVE_FUNC)

View File

@ -151,8 +151,8 @@ if(COMPILER_SUPPORTS_DIAGNOSTICS_COLOR)
endif()
set(EXTRALIBS ${CMAKE_DL_LIBS})
if(NOT APPLE)
list(APPEND EXTRALIBS rt)
if(HAVE_POSIX_TIMERS)
list(APPEND EXTRALIBS ${RT_LIBRARY})
endif()
if(LINUX OR APPLE)
set(LIB_RESOLV resolv)