Some C/C++ code targeted for GNU family compilers fail to compile under Windows due to the dependency on sys/time.h
header file. The repository here has provided a neat implementation for it. Basically you need these three files: time.h
, times.h
and times.cpp
. I have included them here (in case the repository ever went dead). Note that this is not my code and the original license of the code was LGPL.
sys/time.h:
1 2 3 4 5 6 7 |
#pragma once #ifndef _TIMES_H #include "sys/times.h" #endif |
sys/times.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#ifndef _TIMES_H #define _TIMES_H #ifdef _WIN32 #include <sys/timeb.h> #include <sys/types.h> #include <winsock2.h> int gettimeofday(struct timeval* t,void* timezone); // from linux's sys/times.h //#include <features.h> #define __need_clock_t #include <time.h> /* Structure describing CPU time used by a process and its children. */ struct tms { clock_t tms_utime; /* User CPU time. */ clock_t tms_stime; /* System CPU time. */ clock_t tms_cutime; /* User CPU time of dead children. */ clock_t tms_cstime; /* System CPU time of dead children. */ }; /* Store the CPU time used by this process and all its dead children (and their dead children) in BUFFER. Return the elapsed real time, or (clock_t) -1 for errors. All times are in CLK_TCKths of a second. */ clock_t times (struct tms *__buffer); typedef long long suseconds_t ; #endif #endif |
sys/time.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include "sys/times.h" int gettimeofday(struct timeval* t,void* timezone) { struct _timeb timebuffer; _ftime( &timebuffer ); t->tv_sec=timebuffer.time; t->tv_usec=1000*timebuffer.millitm; return 0; } clock_t times (struct tms *__buffer) { __buffer->tms_utime = clock(); __buffer->tms_stime = 0; __buffer->tms_cstime = 0; __buffer->tms_cutime = 0; return __buffer->tms_utime; } |
8 comments
Skip to comment form
Thanks Mehran jan!
It’s very helpful.
With Cuda 8, the following code needs to be added into times.h:
struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};
you can add as well stucture
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* and nanoseconds */
};
Where need I to add the files (“sys/time.h”,…)? Inside the project?
Author
Yes, and note that it should be “sys/time.h” (not) so that it can be found in the local project files (not system include dir).
Hi,
For some reason I’m unable to get this to work as clock_t hasn’t been declared anywhere. It should be in time.h, but hasn’t been in this above case. Any help would be great.
Cheers
James
To follow up on Kyo’s comment, where exactly should these files be placed? For instance, if a package is being installed, and inside this package no such folder “sys” exist, should a folder “sys” be made to place these files?
Thank you very much Mehran Maghoumi!
I use Cygwin to run near Linux system in my Windows laptop. And sys/time.h header file is not included in the version that I am currently using. But using the codes provided in this page, I could get the same result as when the sys/time.h header file were present.