1

The linker keeps telling me that clock_gettime() is undefined.

I have tried using -lrt but then gcc says that it can't find that either.

Am I missing some extra library I needed to download?

// test.c

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

int main()
{
    struct timespec x;
    clock_gettime(CLOCK_REALTIME, &x);
    printf("%d\n", x.tv_sec);
    return 1;
}

When I try to compile this using GCC with MinGW, it puts out this:

gcc test.c
undefined reference to `clock_gettime'

or

gcc test.c -lrt
cannot find -lrt: No such file or directory
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    See e.g. [Is clock_gettime() correctly implemented in MinGW GCC 8.2.0?](https://stackoverflow.com/questions/60020968/is-clock-gettime-correctly-implemented-in-mingw-gcc-8-2-0) – Some programmer dude Feb 17 '23 at 12:36
  • 1
    The function seems to be implemented in Mingw's libpthread library so try `-pthread`. – Ian Abbott Feb 17 '23 at 12:54
  • Please [edit] your question and add details about the version of MinGW you are using. I cannot reproduce the problem on `MINGW64_NT-10.0-19042 HOSTNAME 3.3.6-341.x86_64 2022-09-05 20:28 UTC x86_64 Msys`. I can compile the program with `gcc test.c -o test` and run it without any problem. – Bodo Feb 17 '23 at 12:54
  • @IanAbbott, including pthread worked. thanks for your help :) – fluro red fox Feb 17 '23 at 13:04

0 Answers0