2

I am facing some problems in my raspberry Pi 3 B+ when I compile this simple program that uses time library:

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

int main(int argc, char** argv) { struct timespec now;

clock_gettime(CLOCK_REALTIME, &amp;now);

printf(&quot;Nsecs %d \n&quot;, now.tv_nsec);

return 0;

}

The output of the compilation shows these errors/warnings:

main.c: In function ‘main’: main.c: error: storage size of ‘now’ isn’t known struct timespec now;

main.c: warning: implicit declaration of function ‘clock_gettime’ [-Wimplicit-function-declaration] clock_gettime(CLOCK_REALTIME, &now);

main.c: error: ‘CLOCK_REALTIME’ undeclared (first use in this function) clock_gettime(CLOCK_REALTIME, &now);

I verified that the struct timespec, the method clock_gettime and the constant CLOCK_REALTIME are available in time.h file.

I am compiling with gcc & C99. I am using NetBeans in a laptop to create the code and then I build on the raspberry (no problems so far in other projects). The command is:

gcc -c -g -std=c99 -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.c

What is happening? Could you help me to understand it?

Arcones
  • 123
  • 1
  • 6
  • 2
    This doesn't appear to be a Pi specific question. Could you edit your question and add the command you use to compile? – joan Mar 17 '19 at 12:57

3 Answers3

4

This type of "clock" support isn't C99 but POSIX.

Try removing -std=c99 from your compilation command and compile your code.

If that doesn't work, Add

#define _POSIX_C_SOURCE 199309L

to your code and compile it.

Refer this man page for more details.

Reference

karan
  • 116
  • 3
  • I think this is the correct answer. I will try it in my raspi and come back to mark it as correct if it works. thank you very much – Arcones Mar 21 '19 at 08:39
  • For the record, it worked as soon as I changed in project Properties -> Build -> C Compiler -> C Standard to default value. When I had the error I used to have C99 – Arcones Mar 22 '19 at 19:08
1

Short answer. As per the man pages for clock_gettime you probably need to set a minimum value for _POSIX_C_SOURCE in your code:

#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <time.h>

int main( int argc, char** argv )
  {
    struct timespec now;
    clock_gettime( CLOCK_REALTIME, &now );
    printf( "Nsecs %d \n", now.tv_nsec );
    return 0;
  }

For completeness you may need to link against librt when compiling, but probably not on the Pi:

gcc -c -g -std=c99 -lrt ... -o .../main.o main.c

Note then you could set the _POSIX_C_SOURCE value in your compile line if you prefer (using the -D_POSIX_C_SOURCE=200809L) but this is not recommended as it's not obvious from the source that it's required.

Roger Jones
  • 1,494
  • 7
  • 14
-1
  • Raspberry Pi 3B
  • Ubuntu 16.04
  • Geany IDE

Had to cast now.tv_nsec to an unsigned int. (unsigned int)now.tv_nsec

Run: user1@rpi3b2:~/projects/general/hello_woild$ ./hello_woild Nsecs 847355433

Farad'n
  • 129
  • 1
  • 11