error: ‘CLOCK_MONOTONIC‘ undeclared (first use in this function)

  Error message:

/home/xx/test/main.c: In function ‘main’:
/home/xx/test/main.c:37:21: error: storage size of ‘start’ isn’t known
   37 |     struct timespec start, end; //nanoseconds
      |                     ^~~~~
/home/xx/test/main.c:37:28: error: storage size of ‘end’ isn’t known
   37 |     struct timespec start, end; //nanoseconds
      |                            ^~~
/home/xx/test/main.c:43:5: warning: implicit declaration of function ‘clock_gettime’ [-Wimplicit-function-declaration]
   43 |     clock_gettime(CLOCK_MONOTONIC, &start);
      |     ^~~~~~~~~~~~~
/home/xx/test/main.c:43:19: error: ‘CLOCK_MONOTONIC’ undeclared (first use in this function)
   43 |     clock_gettime(CLOCK_MONOTONIC, &start);
      |                   ^~~~~~~~~~~~~~~
/home/xx/test/main.c:43:19: note: each undeclared identifier is reported only once for each function it appears in
make[2]: *** [CMakeFiles/WBSM4.dir/build.make:63: CMakeFiles/xx.dir/test/main.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/xx.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

Solution:

Add a compiler to the cmakelists.txt file:  

add_compile_options(-D_POSIX_C_SOURCE=199309L) 

Solution source [copyright infringement and deletion]:

c++ – error: ‘CLOCK_ MONOTONIC’ undeclared (first use in this function) – Stack Overflow

error: ‘CLOCK_ Mononic ‘undeclared problem solving_ Whahu 1989 column – CSDN blog

Read More: