tags:

views:

349

answers:

1

I'm trying to determine the granularity of the timers on my Linux box. According to the man pages for clock_getres, I should be able to use this snippet:

#include <time.h>
#include <stdio.h>

int main( int argc, char** argv )
{
  clockid_t types[] = { CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID, (clockid_t) - 1 };

  struct timespec spec;
  int i = 0;
  for ( i; types[i] != (clockid_t) - 1; i++ )
  {
    if ( clock_getres( types[i], &spec ) != 0 )
    {
      printf( "Timer %d not supported.\n", types[i] );
    }
    else
    {
      printf( "Timer: %d, Seconds: %ld Nanos: %ld\n", i, spec.tv_sec, spec.tv_nsec );
    }
  }
}

I'm trying to build like so: gcc -o timertest timertest.c

This works great on Solaris but on Linux I get the error:

/tmp/ccuqfrCK.o: In function `main':
timertest.c:(.text+0x49): undefined reference to `clock_getres'
collect2: ld returned 1 exit status

I've tried passing -lc to gcc, apparently clock_getres is defined in libc, but it makes no difference. I must be missing something simple here - any ideas?

Thanks,

Russ

+4  A: 

You need to link with with RT library (-lrt)

Nikolai N Fetissov
excellent, thanks.
Even on Linux, the manpage for clock_getres states "Link with -lrt." Russ, you're just lucky that Linux is as forgiving as it is.
ephemient