Obtendo tempo no Ubuntu [fechado]

1
#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);

}

Estou tentando compilar o código acima, mas ele continua enviando o erro:

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]

Eu tentei compilar com -lrt flag e -std = gnu99. Nada funciona.

    
por user2578666 13.06.2014 / 11:24

1 resposta

0

Substitua #include <sys/time.h> por #include <time.h>

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

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

Outro bom exemplo: link

    
por TuKsn 13.06.2014 / 11:49

Tags