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



在Windows上,版本为Code::Block,版本为16.01,版本为minwg32 gcc。

这是代码:

#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;rn", t.tv_sec, t.tv_usec);
return 0;
}

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

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

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

-------------- Build: Debug in TestForStudy (编译器: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: 在函数 'gettimeofday_ex' 中: D:\WorkSpace\iSource\TestForStudy\main.c:60:13: 警告:函数"_tzset"的隐式声明 [-隐式函数声明] _tzset(); ^ D:\WorkSpace\iSource\TestForStudy\main.c:63:30: error: '_timezone' undeclaration(首次在此函数中使用) tz->tz_minuteswest = _timezone/60; ^ D:\WorkSpace\iSource\TestForStudy\main.c:63:30: 注意:每个未声明 标识符仅针对其出现的每个函数报告一次 D:\WorkSpace\iSource\TestForStudy\main.c:64:26: error: '_daylight' 未声明(首次在此函数中使用) tz->tz_dsttime = _daylight; ^ 进程终止,状态为 1 (0 分钟, 0 秒) 2 错误, 1 警告 (0 分钟), 0 秒)

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

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

编辑:

在我发布这个问题后,我读到了这个问题: 当我使用 -std=c99 [c] 时未声明套接字

这似乎是同样的问题,我尝试将 -std=c99 更改为 -std=gnu99,它再次工作。

那么,这是带有 c99 标志的 minwg32 中的错误吗?因为我认为没有哪个标志,代码没有任何错误,所有的标志都不应该有错误。

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

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

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

我个人建议始终使用 ISO C 标准之一设置的-std标志进行编译,最好是最新的 (-std=c11)。然后,如果您需要使用扩展名,请在仅为需要扩展的文件#include任何标准标头之前定义启用扩展的功能测试宏,例如,在使用POSIX.1-2001函数的文件中,请使用以下第一行:

#define _POSIX_C_SOURCE 200112L

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

最新更新