undefined reference to `clock_gettime'

Below given program produces error:
try.c:(.text+0xf): undefined reference to `clock_gettime’

Please can anybody , tell me how to get this compiled successfully!!!

1 #include <time.h>
2 #include <stdio.h>
3
4 int main(int argc, char * argv)
5 {
6
7 // get starting time
8 struct timespec stop;
9 clock_gettime(CLOCK_REALTIME,&stop);
10
11 return 0;
12 }

Hi ganeshp,

Add “-lrt” to the link line.

  • Mat
% cat clock.c
#include <time.h>
#include <stdio.h>

int main(int argc, char * argv[])
{

// get starting time
struct timespec stop;
clock_gettime(CLOCK_REALTIME,&stop);

 return 0;
}
% pgcc clock.c
/tmp/pgccbwcfdCA5rZ-S.o(.text+0x55): In function `main':
: undefined reference to `clock_gettime'
% pgcc clock.c -lrt
clock.c:
%