c - 当我将标志 "-std=c99"添加到 minwg32 时会发生什么?

标签 c c99

在带有 Code::Block 版本 16.01 和 minwg32 gcc 的 Windows 上。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <windows.h>

#ifdef WIN32
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
#else
#define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
#endif
#endif

struct timezone_ex
{
    int  tz_minuteswest; /* minutes W of Greenwich */
    int  tz_dsttime;     /* type of dst correction */
};

int gettimeofday_ex(struct timeval *tv, struct timezone_ex *tz);

/********************************************//**
 * \brief
 *
 * \param tv struct timeval*
 * \param tz struct timezone*
 * \return int
 *
 ***********************************************/
int gettimeofday_ex(struct timeval *tv, struct timezone_ex *tz)
{
    FILETIME ft;
    unsigned long long tmpres = 0;
    static int tzflag;

    if (tv)
    {
        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 = (long)(tmpres / 1000000UL);
        tv->tv_usec = (long)(tmpres % 1000000UL);
    }
    if (tz)
    {
        if (!tzflag)
        {
            _tzset();
            tzflag++;
        }
        tz->tz_minuteswest = _timezone / 60;
        tz->tz_dsttime = _daylight;
    }

    return 0;
}

int main()
{
    struct timeval t;

    gettimeofday_ex(&t, NULL);

    printf("t.tv_sec=%d, t.tz_dsttime=%d;\r\n", t.tv_sec, t.tv_usec);

    return 0;
}

当我在没有标志 -std=c99 的情况下编译代码时,它是有效的; 这是构建日志:

-------------- Build: Debug in TestForStudy (compiler: GNU GCC Compiler)---------------

mingw32-gcc.exe -g -c D:\WorkSpace\iSource\TestForStudy\main.c -o obj\Debug\main.o mingw32-g++.exe -o bin\Debug\TestForStudy.exe obj\Debug\main.o -lpthread Output file is bin\Debug\TestForStudy.exe with size 40.23 KB Process terminated with status 0 (0 minute(s), 0 second(s)) 0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

但是,如果我添加标志 -std=c99,并再次重建它,我会收到错误消息:

-------------- Build: Debug in TestForStudy (compiler: GNU GCC Compiler)---------------

mingw32-gcc.exe -std=c99 -g -c D:\WorkSpace\iSource\TestForStudy\main.c -o obj\Debug\main.o mingw32-g++.exe -o bin\Debug\TestForStudy.exe obj\Debug\main.o -lpthread D:\WorkSpace\iSource\TestForStudy\main.c: In function 'gettimeofday_ex': D:\WorkSpace\iSource\TestForStudy\main.c:60:13: warning: implicit declaration of function '_tzset' [-Wimplicit-function-declaration] _tzset(); ^ D:\WorkSpace\iSource\TestForStudy\main.c:63:30: error: '_timezone' undeclared (first use in this function) tz->tz_minuteswest = _timezone / 60; ^ D:\WorkSpace\iSource\TestForStudy\main.c:63:30: note: each undeclared identifier is reported only once for each function it appears in D:\WorkSpace\iSource\TestForStudy\main.c:64:26: error: '_daylight' undeclared (first use in this function) tz->tz_dsttime = _daylight; ^ Process terminated with status 1 (0 minute(s), 0 second(s)) 2 error(s), 1 warning(s) (0 minute(s), 0 second(s))

我用谷歌搜索了一些关于这个问题的东西,但没有得到任何有用的信息。我不知道是我的代码有问题还是 minwg32 有问题?

任何人都可以给我任何关于这个问题的提示吗? 谢谢!

编辑:

在我发布这个问题之后,我阅读了这个问题: Socket undeclared when i use -std=c99 [c]

好像是同样的问题,我尝试把-std=c99改成-std=gnu99,又可以了。

那么,这是带有标志 c99 的 minwg32 中的错误吗?因为我觉得不管用哪个flag,代码都没有错误,所有的flag都不应该有错误。

最佳答案

当您在命令行中指定特定的 C 标准版本时,标准头文件的所有非标准内容都会被“隐藏”。这就是 tzset 发生的事情.它在 <time.h> 中声明, 但它不是 <time.h> 的标准成员.标准库没有这个功能。

如果你想让你的编译器在<time.h>中看到它并使用它,指定 -std=gnu99而不是 -std=c99 .如果您希望您的程序符合标准,请使用 -std=c99忘记tzset .要么这样要么那样。

关于c - 当我将标志 "-std=c99"添加到 minwg32 时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44733462/

相关文章:

c - 通过生成中间阶段执行大型 C 程序

c - 使用复合文字初始化变量

c++ - "&a+1 > &a"会导致未定义的行为吗

C++ 确保绝对没有标准输入缓冲

c - 在 c 中发出读取和打印 ip header ID

c - 如何用表格制作 "global variable"?

c - 当用换行符分隔时,字符串文字是否一定被视为相邻(并因此连接)?

c - 有谁知道什么是亲缘关系?

C添加已由函数初始化的结构覆盖原始结构

c - _Bool 的 printf 转换说明符?