J
J
jov2012-09-07 02:49:52
CMake
jov, 2012-09-07 02:49:52

Weirdness with CHECK_C_SOURCE_COMPILES macro from CMake

I am translating the project, or rather part of it, to cmake. To check for the presence of a function, previously used

AC_MSG_CHECKING(for function clock_gettime in time.h)
AC_TRY_LINK([
#include <time.h>
],
[struct timespec tp;
clock_gettime(CLOCK_REALTIME, &tp);
],
AC_DEFINE(HAVE_TIME_CLOCK_GETTIME,1,[Define to 1 if function 'clock_gettime' exists.])
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))
On my host, checking shows the presence of this feature.
Now I'm trying to check the availability of this function in the following ways
INCLUDE (CheckFunctionExists)
CHECK_FUNCTION_EXISTS(clock_gettime HAVE_TIME_CLOCK_GETTIME)
and
INCLUDE (CheckCSourceCompiles)
CHECK_C_SOURCE_COMPILES("#include <time.h>
 int main(int argc, char * argv[]) {
  struct timespec tp;
  clock_gettime(CLOCK_REALTIME, &tp))
}" HAVE_TIME_CLOCK_GETTIME)
In both cases, failure. Please tell me what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2012-09-08
@phprus

According to the documentation, the clock_gettime function requires linking with the rt library (Link with -lrt), so cmake cannot link the test code.
According to the cmake documentation, you can use the CMAKE_REQUIRED_LIBRARIES variable to ensure that the above functions include the required library when linking.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question