簡體   English   中英

將C程序從Linux遷移到Windows

[英]Migrating c program from linux to windows

我將在Linux上編寫的工作程序遷移到Windows時遇到問題。 我整理了一下,但是下面的代碼片段總是給我一個錯誤。

代碼段:

...
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

如果有人可以幫助我,我將不勝感激!

@Dave感謝您將鏈接添加到代碼段下面的代碼中:

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;
}

並且代碼正確構建:)!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM