How come CHECK_FUNCTION_EXISTS doesn’t find clock_gettime?
I use the following code in my CMakeLists.txt:
include(CheckFunctionExists)
set(CMAKE_EXTRA_INCLUDE_FILES time.h)
CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME)
This is on a POSIX system I know has clock_gettime. Yet I simply get:
-- Looking for clock_gettime - not found
Because
clock_gettimeis found inlibrtwe need to link to that when doing the check (otherwise CMake will simply fail to compile the test program it generates to test if the function exists).This is not possible with
CHECK_FUNCTION_EXISTS. Instead CHECK_LIBRARY_EXISTS must be used:This will now work and output:
Update: In newer glibc 2.17+
clock_gettimehas been moved fromlibrttolibc.So to be sure to find
clock_gettimeon all systems you would need to do two checks: