Получение времени в Ubuntu

Получение времени в Ubuntu
#include <sys/time.h>
#include <stdio.h>


int GetTime()
{

   struct timespec tsp;

   clock_gettime(CLOCK_REALTIME, &tsp);   //Call clock_gettime to fill tsp

   fprintf(stdout, "time=%d.%d\n", tsp.tv_sec, tsp.tv_nsec);

   fflush(stdout);

}

Я пытаюсь скомпилировать приведенный выше код, но он продолжает выдавать ошибку:

time.c: In function ‘GetTime’:
time.c:12:4: warning: implicit declaration of function ‘clock_gettime’ [-Wimplicit-function-declaration]
time.c:12:18: error: ‘CLOCK_REALTIME’ undeclared (first use in this function)
time.c:12:18: note: each undeclared identifier is reported only once for each function it appears in
time.c:14:4: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘__time_t’ [-Wformat]
time.c:14:4: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long int’ [-Wformat]

Я пробовал компилировать с флагом -lrt и -std=gnu99. Ничего не работает.

решение1

Заменить#include <sys/time.h>#include <time.h>

и fprintf(stdout, "time=%d.%d\n", tsp.tv_sec, tsp.tv_nsec);с

fprintf(stdout, "time=%d.%d\n", (int) tsp.tv_sec, (int) ttsp.tv_nsec);

Еще один хороший пример:http://ubuntuforums.org/showthread.php?t=1244004&p=7812845#post7812845

Связанный контент