c++visual-studio-2008timeportingtimeval

Porting time APIs from Linux to Visual Studio 2008


I have an application that I am porting to Microsoft Visual Studio 2008 that builds and runs fine on Linux.

I am having trouble with the time routines, my Linux code looks like this:

#include <sys/types.h>
#include <sys/time.h>

typedef long long Usec;

inline Usec timevalToUsec(const timeval &tv)
{
  return (((Usec) tv.tv_sec) * 1000000) + ((Usec) tv.tv_usec);
}

But the compiler fails on the sys/time.h header file:

fatal error C1083: Cannot open include file:
      'sys/time.h': No such file or directory

If I change the include to just time.h I get a different error with timeval not being defined:

error C4430: missing type specifier - int assumed.
      Note: C++ does not support default-int

This is due to the timeval not being defined.

Is including time.h instead of sys/time.h correct, and if so, where do I get the definition of struct timeval in Microsoft Visual Studio 2008?


Solution

  • The winsock2.h header fill will pull in struct timeval since it's used in calls like select.