Das Folgende funktioniert bei mir unter CentOS 6:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#if ! defined(_POSIX_C_SOURCE)
# error "_POSIX_C_SOURCE undefined"
#endif
#if _POSIX_C_SOURCE < 199309L
# error "_POSIX_C_SOURCE < 199309L"
#endif
int main(int, char**){
struct timespec start, stop;
struct timespec stop;
if(clock_gettime(CLOCK_MONOTONIC, &start )) goto err_out;
if(clock_gettime(CLOCK_MONOTONIC, &stop )) goto err_out;
printf("start == {tv_sec: %d, tv_nsec: %d}\n", start.tv_sec, start.tv_nsec);
printf("stop == {tv_sec: %d, tv_nsec: %d}\n", stop.tv_sec, stop.tv_nsec );
printf("stop.tv_nsec - start.tv_nsec == %d\n", stop.tv_nsec - start.tv_nsec);
return EXIT_SUCCESS;
err_out:
perror("clock_gettime");
return EXIT_FAILURE ;
}
...auch wenn dies Folgendes erfordert librt
:
$ make dur
cc dur.c -o dur
/tmp/cc1yF58x.o: In function `main':
dur.c:(.text+0x1c): undefined reference to `clock_gettime'
dur.c:(.text+0x4e): undefined reference to `clock_gettime'
collect2: ld returned 1 exit status
make: *** [dur] Error 1
$ LDFLAGS="-lrt" make dur
cc -lrt dur.c -o dur
$ ./dur
start == {tv_sec: 206247, tv_nsec: 411717209}
stop == {tv_sec: 206247, tv_nsec: 411759791}
stop.tv_nsec - start.tv_nsec == 42582