简体   繁体   English

MinGW不会发出警告

[英]MinGW doesn't produce warnings

I have successfully installed MinGW on a Windows 7 32bit machine, and have tried to compile a simple program using either the command line or the MinGW console. 我已经在Windows 7 32位机器上成功安装了MinGW,并尝试使用命令行或MinGW控制台编译一个简单的程序。

The code has an intentional error in a printf statement: 代码在printf语句中有故意错误:

#include <stdio.h>
#include <stdlib.h>
int main( void )
{
    printf("%d\n" , 3.14 ) ;
return 0 ;
}

The command gcc -Wall hello.c gives a correct warning: hello.c:7:2: warning: format '%d' expects argument of type 'int'... 命令gcc -Wall hello.c给出了正确的警告: hello.c:7:2:警告:格式'%d'需要类型'int'的参数...

But the command gcc -std=c99 -Wall hello.c doesn't give any warning. 但是命令gcc -std=c99 -Wall hello.c没有给出任何警告。

Both create an executable a.exe ( that runs and gives the same result ). 两者都创建一个可执行文件a.exe (运行并给出相同的结果)。

(Interestingly a command gcc -std=gnu99 -Wall hello.c gives the warning.) (有趣的是,命令gcc -std=gnu99 -Wall hello.c会发出警告。)

I don't know if this is a bug, or did the installation go wrong somehow, but both seem unlikely since the compiler works and successfully compiled a larger project( but the same warning of course omitted when using -std=c99 ). 我不知道这是一个错误,还是安装出错了,但两者似乎都不太可能,因为编译器工作并成功编译了一个更大的项目(但当使用-std = c99时,同样的警告当然会被忽略)。

I must be missing some information. 我一定错过了一些信息。

(ps: If someone has a new MinGW install, please test this.) (ps:如果有人安装了新的MinGW,请测试一下。)

gcc version 4.8.1 (GCC) gcc版本4.8.1(GCC)

Update 1: 更新1:

Defining _GNU_SOURCE before including stdio.h removes the warning even with gcc -Wall hello.c . 在包含stdio.h之前定义_GNU_SOURCE即使使用gcc -Wall hello.c也会删除警告。

Update 2( might be less relevant ): 更新2(可能不太相关):

Compiling 编译

 printf("%lf\n" , 3.14 ) ;

-std=c99 flag outputs: 0.000000 -std=c99标志输出:0.000000

-std=gnu99 outputs: 3.140000 -std=gnu99输出:3.140000

And compiling: 并编译:

 printf("%f\n" , 3.14 ) ;

-std=gnu99 and -std=c99 output: 3.140000 -std=gnu99-std=c99输出:3.140000

Update 3: 更新3:

Functions that seem to be affected are: printf, fprintf, snprintf, sprintf. 似乎受影响的函数是:printf,fprintf,snprintf,sprintf。

The problem with the lack of warning when using the std=c99 option looks like it's because MinGW 4.8.1 preprocesses stdio.h a little different for the printf() family of functions when -std=c99 is used compared to when -std=gnu99 is used. 使用std=c99选项时缺少警告的问题看起来像是因为当使用-std=c99时,MinGW 4.8.1预处理stdio.hprintf()函数系列略有不同,而当-std=gnu99

Note: I'm looking at MinGW 4.8.1 from TDM - I think other distributions might differ in these details. 注意:我正在从TDM看MinGW 4.8.1 - 我认为其他发行版可能在这些细节上有所不同。

MinGW has had some compatibility issues with formatting floating point values because of its historic reliance on msvcrt.dll for the C runtime and the fact that MSVC uses a 64-bit representation for long double while gcc uses a 96-bit (or 128-bit on x64) representation. MinGW在格式化浮点值方面存在一些兼容性问题,因为它历史上依赖于msvcrt.dll用于C运行时以及MSVC使用64位表示形式进行long double精度而gcc使用96位(或128位)这一事实在x64)表示。 See gcc: printf and long double leads to wrong output. 请参阅gcc:printf和long double导致输出错误。 [C - Type conversion messes up] for some details. [C - 类型转换混乱]了解一些细节。 More recent versions of MinGW have provided thier own implementation of the printf() family of functions (with a __mingw_ prefix on the name) in libmingwex.a to solve those problems. MinGW的更新版本已经在libmingwex.a提供了自己的printf()系列函数(名称上带有__mingw_前缀)来解决这些问题。

The header files _mingw.h and stdio.h configure whether or not the libmingwex.a implementations or the msvcrt.dll implementations will be used. 头文件_mingw.hstdio.h配置是否使用libmingwex.a实现或msvcrt.dll实现。

It appears that if ANSI compliance is requested, MinGW will use the libmingwex.a implementations (there are a number of other ways to get this configuration too - look at the headers for details). 看来如果要求ANSI合规性,MinGW将使用libmingwex.a实现(还有许多其他方法来获取此配置 - 请查看标题以获取详细信息)。 Wiring up a user call to printf() to the __mingw_printf() implementation in libmingwex.a is done by stdio.h defining a static inline implementation of printf() that is a thin wrapper around a call to __mingw_vfprintf() . 接线,用户调用printf()__mingw_printf()实施libmingwex.a被做stdio.h定义的静态连续实施printf()是围绕一个电话薄包装到__mingw_vfprintf() Apparently the -Wformat doesn't get applied to versions of printf() family functions that the compiler doesn't believe to be part of the library (a reasonable assumption - the compiler doesn't really know anything about those functions). 显然-Wformat没有应用于printf()系列函数的版本,编译器不相信它们是库的一部分(一个合理的假设 - 编译器并不真正了解这些函数)。 This problem can be fixed by applying the appropriate function attribute (for example: __attribute__ ((format (printf, 1, 2))) ) to the static inline wrapper functions. 可以通过将适当的函数属性(例如: __attribute__ ((format (printf, 1, 2))) )应用于静态内联包装函数来解决此问题。

The other problem you found, where printf("%lf\\n", 3.14) prints 0.000000 when using std=c99 , looks to be a bug in the libmingwex.a implementation of __mingw_vfprintf() . 你发现的另一个问题是,当使用std=c99printf("%lf\\n", 3.14)打印0.000000 ,看起来是__mingw_vfprintf()libmingwex.a实现中的一个错误。 It seems that __mingw_vfprintf() mistakenly interprets "%lf" to mean the argument is a long double . 似乎__mingw_vfprintf()错误地将"%lf"解释为意味着参数是一个long double I'm not too surprised by that - I always have to look up whether %lf means double or long double . 我对此并不感到惊讶 - 我总是要查看%lf是表示double还是long double

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM