【问题标题】:Migrating c program from linux to windows将c程序从linux迁移到windows
【发布时间】:2014-10-24 13:53:22
【问题描述】:

我在将 linux 上编写的工作程序迁移到 windows 时遇到问题。我稍微清理了一下,但是下面的代码 sn-p 总是给我一个错误。

代码sn-p:

...
struct timeval mytime;
gettimeofday(&mytime, (struct timezone*)0);
tseconds = (double) (mytime.tv_sec + mytime.tv_usec*1.0e-6);
...

堆栈跟踪:

Error   1   error C2079: 'mytime' uses undefined struct 'timeval'
Warning 2   warning C4013: 'gettimeofday' undefined; assuming extern returning int
Error   3   error C2224: left of '.tv_sec' must have struct/union type
Error   4   error C2224: left of '.tv_usec' must have struct/union type
5   IntelliSense: incomplete type is not allowed
6   IntelliSense: incomplete type is not allowed
7   IntelliSense: incomplete type is not allowed

如果有人可以帮助我,我将非常感激!

【问题讨论】:

  • 您是否在源代码之上执行了正确的#include 语句? Windows 中的某些 .h 文件与 Unix 中的不同。
  • 你可以使用像Glib这样的框架,或者(如果你可以用C++编写代码)pocoboostQt...

标签: c linux windows visual-studio-2013 openmp


【解决方案1】:

@Dave 感谢我在 sn-ps 下面添加到我的代码中的链接:

const __int64 DELTA_EPOCH_IN_MICROSECS = 11644473600000000;
struct timeval2 {
    __int32 tv_sec;
    __int32 tv_usec;
};
struct timezone2
{
    __int32  tz_minuteswest; /* minutes W of Greenwich */
    bool  tz_dsttime;     /* type of dst correction */
};
int gettimeofday(struct timeval2 *tv/*in*/, struct timezone2 *tz/*in*/)
{
    FILETIME ft;
    __int64 tmpres = 0;
    TIME_ZONE_INFORMATION tz_winapi;
    int rez = 0;

    ZeroMemory(&ft, sizeof(ft));
    ZeroMemory(&tz_winapi, sizeof(tz_winapi));

    GetSystemTimeAsFileTime(&ft);

    tmpres = ft.dwHighDateTime;
    tmpres <<= 32;
    tmpres |= ft.dwLowDateTime;

    /*converting file time to unix epoch*/
    tmpres /= 10;  /*convert into microseconds*/
    tmpres -= DELTA_EPOCH_IN_MICROSECS;
    tv->tv_sec = (__int32) (tmpres*0.000001);
    tv->tv_usec = (tmpres % 1000000);


    //_tzset(),don't work properly, so we use GetTimeZoneInformation
    rez = GetTimeZoneInformation(&tz_winapi);
    tz->tz_dsttime = (rez == 2) ? true : false;
    tz->tz_minuteswest = tz_winapi.Bias + ((rez == 2) ? tz_winapi.DaylightBias : 0);

    return 0;
}

并且代码构建正确:)!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-11
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    相关资源
    最近更新 更多