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, &now);

    printf("Nsecs %d \n", 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?

1
  • 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
    Commented Mar 17, 2019 at 12:57

3 Answers 3

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

2
  • 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
    Commented Mar 21, 2019 at 8: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
    Commented Mar 22, 2019 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.

-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

2
  • 1
    Thanks, but is not that. The problem is in the time.h imported items.
    – Arcones
    Commented Mar 17, 2019 at 15:40
  • 1
    I do not understand this cryptic lines. What do you want to explain to solve the problem?
    – Ingo
    Commented Mar 19, 2019 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.