1

I read the following manual: http://linux.die.net/man/3/clock_gettime

and I wrote the following code:

#include <time.h>
int main() { 
    struct timespec clk;
    clock_gettime(CLOCK_REALTIME, &clk);
    return 0;
}

Surprisingly, I get the following errors:

Symbol CLOCK_REALTIME could not be resolved

undefined reference to clock_gettime

I still don't understand what is the problem. I included the header, and these names show in this header.

CrazySynthax
  • 9,442
  • 21
  • 70
  • 136
  • Works for me... What Linux distro and version are you using? Are you sure all packages for C development are correctly installed? What's the command line you use for compiling the program? – fvu Nov 21 '15 at 17:06
  • I have Ubuntu 12.04. How can I check if all packages for C development are installed? The command lines is: gcc -o "example" ./main.o and I'm doing it by eclipse. – CrazySynthax Nov 21 '15 at 17:08
  • could you try, from the commandline `gcc -o somename main.c` ? I don't know enough about ubuntu to help you with the packages that must be present, sorry. – fvu Nov 21 '15 at 18:35
  • In which time.h did you look? I could only extract the source archive of package libc6-dev at the moment. CLOCK_REALTIME is not defined in /usr/include/time.h there. – Martin Zabel Nov 21 '15 at 19:13
  • This will install the required packages : `sudo apt-get update && sudo apt-get install g++` . I.e. the dependencies libc6-dev, linux-libc-dev, ( libgcc-4.x-dev, ). – Knud Larsen Nov 21 '15 at 19:14

1 Answers1

1

maybe you should use#define _POSIX_TIMERS,#define _REENTRANT besides, when you compile the code, make sure to link the real-time library which is cc filename.c -o filename -lrt

Update 1.0:
sometimes in windows or mac os, C ide may not include real-time library automatically, or we may not used the posix directly without _POSIX_TIMES, therefore you have to link the real-time library manually. In Linux, you can just type in cc filename.c -o filename -lrt to compile the c file.

C. zero
  • 41
  • 3