【问题标题】:What happens when I add the flag "-std=c99" to minwg32?当我将标志“-std=c99”添加到 minwg32 时会发生什么?
【发布时间】:2017-11-27 17:51:04
【问题描述】:

在带有 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 的代码时,它可以工作; 这是构建日志:

------------- 构建:TestForStudy 中的调试(编译器:GNU GCC 编译器)---------------

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 输出文件为 bin\Debug\TestForStudy.exe 大小为 40.23 KB 进程终止于 状态 0(0 分钟,0 秒)0 错误,0 警告(0 分钟,0 秒)

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

------------- 构建:TestForStudy 中的调试(编译器:GNU GCC 编译器)---------------

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:在函数“gettimeofday_ex”中:D:\WorkSpace\iSource\TestForStudy\main.c:60:13: 警告:函数“_tzset”的隐式声明 [-Wimplicit-function-declaration] _tzset(); ^ D:\WorkSpace\iSource\TestForStudy\main.c:63:30: error: '_timezone' undeclared (第一次在这个函数中使用) tz->tz_minuteswest = _timezone / 60; ^ D:\WorkSpace\iSource\TestForStudy\main.c:63:30:注意:每个未声明 标识符对于它出现的每个函数只报告一次 D:\WorkSpace\iSource\TestForStudy\main.c:64:26:错误:'_daylight' 未声明(在此函数中首次使用) tz->tz_dsttime = _daylight; ^ 进程以状态 1 终止(0 分钟,0 秒)2 错误,1 警告(0 分钟,0 秒)

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

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

编辑:

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

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

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

【问题讨论】:

    标签: c c99


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      添加到 AnT 的答案:C 标准库有 不同 扩展,有些是在 POSIX 中指定的,有些是 BSD 扩展,有些来自 System V Unix 和当然还有 GNU 特定的扩展,仅举几例。

      我个人建议始终将 -std 标志设置为 ISO C 标准之一,最好是最新的 (-std=c11)。然后,如果您需要使用扩展,请在 #include 之前定义 feature test macro 启用它们,仅用于需要它们的文件的任何标准头,例如在使用POSIX.1-2001 函数的文件中,使用第一行:

      #define _POSIX_C_SOURCE 200112L
      

      此建议的原因是明确wherewhich 对语言的扩展被使用。这简化了以后移植到不同平台的过程。它还鼓励您将依赖于扩展的代码与完全可移植的代码清楚地分开。

      【讨论】:

        猜你喜欢
        • 2010-11-28
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 2020-05-17
        • 1970-01-01
        • 2019-04-20
        • 2019-05-27
        • 1970-01-01
        相关资源
        最近更新 更多