Porting time APIs from Linux to Visual Studio 2008

你。 提交于 2019-12-06 05:17:54

问题


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?


回答1:


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




回答2:


It exists, just not in "sys/time.h". Timeval is in, interestingly, Winsock2.h.



来源:https://stackoverflow.com/questions/8301370/porting-time-apis-from-linux-to-visual-studio-2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!