(I know what you're asking about glibc , but you really want to know if clock_gettime is clock_gettime , right? But nothing in your question depends on Linux ...)
If you want to check clock_gettime , you can use a preprocessor. If clock_gettime present, then _POSIX_TIMERS will be defined. The clock_gettime function is part of the optional POSIX extension ( see Specification ), so it is not Linux-specific, but not universal. Mac OS X does not have clock_gettime : it is not declared in any header, nor defined in any library.
#include <time.h> #include <unistd.h> #if _POSIX_TIMERS ...use clock_gettime()... #else ...use something else... #endif
This does not solve the problem that you still need to mess with -lrt on Linux. This is usually resolved with AC_CHECK_LIB in Autoconf, I'm sure there is an equivalent in CMake.
From man 2 clock_gettime :
On POSIX systems on which these functions are available, the _POSIX_TIMERS character _POSIX_TIMERS defined in <unistd.h> value greater than 0. The characters _POSIX_MONOTONIC_CLOCK , _POSIX_CPUTIME , _POSIX_THREAD_CPUTIME indicate that CLOCK_PROCESS_CPUTIME_ID , CLOCK_THREAD_CPUTIME_ID . (See also sysconf (3).)
In Darwin, you can use the mach_absolute_time function if you need a high-resolution monotonous clock. If you do not need resolution or monotony, you should probably use gettimeofday on both platforms.
source share