title: How to use Linux-specific APIs and libraries only on Linux builds with CMake?
body: <p>I have a project that I run on Linux (primarily), but sometimes on Darwin/Mac OS X. I use CMake to generate Makefiles on Linux and an Xcode project on Mac OS X. So far, this has worked well.</p>
  
  <p>Now I want to use some Linux-specific functions (<code>clock_gettime()</code> and related functions). I get linker errors on Mac OS X when I try to use <code>clock_gettime()</code>, so I assume it is only available on Linux. I am prepared to introduce conditionally-compiled code in the .c files to use <code>clock_gettime()</code> on Linux and plain old <code>clock()</code> on Mac OS. (BTW I was planning to use <code>#include &lt;unistd.h&gt;</code> and <code>#if _POSIX_TIMERS &gt; 0</code> as the preprocessor expression, unless someone has a better alternative.)</p>
  
  <p>Things get tricky when it comes to the CMakeLists.txt file. What is the preferred way of introducing linkage to Linux-specific APIs only under the Linux build in a cross-platform CMake project?</p>
  
  <p>Note: An earlier revision of this question contained references to glibc, which was overly specific and confusing. The question is really about the right way to use Linux-specific APIs and libraries in a cross-platform CMake project.</p>