0

I'm calling clock_gettime with CLOCK_REALTIME to measure the time elapsed for a program of mine. The code is like this:

clock_gettime(CLOCK_REALTIME, &start);
int retCode = system(completeString.c_str());
clock_gettime(CLOCK_REALTIME, &finish);

and it gives me this errors when i try to compile it with gcc -lrt -o timeSpent.exe time.cpp:

time.cpp: In function 'int main(int, char**)':

time.cpp:38:18: error: 'CLOCK_REALTIME' was not declared in this scope clock_gettime(CLOCK_REALTIME, &start);

time.cpp:38:40: error: 'clock_gettime' was not declared in this scope clock_gettime(CLOCK_REALTIME, &start);



Header files:

#include <iostream>
#include <sstream>
#include <fstream>
#include <ctime>
#include <Windows.h>


Compiler: MinGW

I hope one of you guys is able to help me fix this problem, and i'm grateful for anyone who tries to help.

Ps: I'm using Windows 10, but i have been successful on compiling this same code before on a Win10 PC. Unfortunately i can't access it, since I'm in another country.


Ps²: I've already tried to #define _POSIX_C_SOURCE 199309L, the only thing it does is add a warning.

  • Which header files are you including? Which compiler are you using? Also, that isn't a C++ Standard Library function.. –  Jan 01 '19 at 22:19
  • You should see the answer [here](https://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows) – tunglt Jan 01 '19 at 22:51
  • @NeilButterworth, i edited the post with the information you asked. – Nicholas Lopes Jan 01 '19 at 23:04
  • @tunglt i tried out the clock_gettime from the post you shared, and one from Carl Staelin worked out, but it still doesn't fix the CLOCK_REALTIME or CLOCK_MONOTONIC issue. Any insights? Thanks for your help <3 – Nicholas Lopes Jan 01 '19 at 23:05
  • 2
    Do you add `#define _POSIX_C_SOURCE 199309L` before including the header file `#include ` ? – tunglt Jan 01 '19 at 23:15
  • I tried adding `#define _POSIX_C_SOURCE 199309L` before the header file `#include `, time.h is not used – Nicholas Lopes Jan 01 '19 at 23:26
  • mingw introduce clock_gettime() in the mingw64 version and this function is not available for 32bit version. If you really want to use it, you should : 1. Move to mingw64, or 2. reimplement it, see the source code [here](https://github.com/mirror/mingw-w64/blob/master/mingw-w64-libraries/winpthreads/src/clock.c#L109) – tunglt Jan 02 '19 at 08:31

0 Answers0