lundi 23 octobre 2023

How to link c++11 std::chronos to use clock_gettime()

I want the std::chrono::steady_clock and std::chrono::system_clock to use POSIX functions clock_gettime/clock_settime from the <time.h>, instead it is picking up _gettimeofday() from <sys/time.h>.

When I peaked into the src of stdlib here: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/src/c%2B%2B11/chrono.cc And the posix header file <time.h> in the compiler, I concurred that I need to add the following flags to the make file (to allow std::chrono to use clock_gettime()).

CXXFLAGS +=
-D_GLIBCXX_USE_CLOCK_MONOTONIC
-D_GLIBCXX_USE_CLOCK_REALTIME
-D_POSIX_TIMERS
-D_POSIX_MONOTONIC_CLOCK \

I defined these functions in time.c file and included them in the build. Linked them as given below:

LIB_NAMES +=
<my_libraries>
c \

I'm using gcc 8.3 & newlibc.

I expect the calls to std::chrono::steady_clock::now(), std::chrono::system_clock::now() to use clock_gettime() function instead of _gettimeofday().

I have an accurate hardware time that is being accessed from clock_gettime(). I have verified this by directly calling it in application functions. I'd like to use c++ std::chrono instead.

What am I missing?

Aucun commentaire:

Enregistrer un commentaire