How to use Linux-specific APIs and libraries only for Linux created with CMake?

I have a project that I run on Linux (first), but sometimes on Darwin / Mac OS X. I use CMake to create a Makefile on Linux and an Xcode project on Mac OS X. So far, this has worked well.

Now I want to use some Linux-specific functions ( clock_gettime() and related functions). I get linker errors on Mac OS X when I try to use clock_gettime() , so I assume it is only available on Linux. I am ready to enter conditionally compiled code in .c files to use clock_gettime() on Linux and the plain old clock() on Mac OS. (BTW I planned to use #include <unistd.h> and #if _POSIX_TIMERS > 0 as an expression for the preprocessor, unless someone has a better alternative.)

Everything gets complicated when it comes to the CMakeLists.txt file. What is the preferred way to only bind to the Linux APIs as part of the Linux build in the cross-platform CMake project?

Note. An earlier version of this question contained links to glibc that were overly specific and confusing. The question is how to properly use Linux-specific APIs and libraries in a cross-platform CMake project.

+4
source share
3 answers

Distracting from your examples and answering only this question:

How to use Linux specific APIs and libraries only for Linux with CMake?

CMake provides numerous useful constants that you can check to determine which system you are running on:

 if (${UNIX}) # *nix-specific includes or actions elsif (${WIN32}) # Windows-specific includes or actions elsif (${APPLE}) # ... endif (${UNIX}) 
+7
source

(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> /* for _POSIX_TIMERS definition, if present */ #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.

+5
source

There is also a built-in CMake macro to check for the presence of a character - CheckSymbolExists .

0
source

Source: https://habr.com/ru/post/1389858/


All Articles