MinGW的不产生警告 [英] MinGW doesn't produce warnings
问题描述
我已成功安装MinGW的Windows 7的32位机器上,并试图编译使用在命令行或MinGW的控制台一个简单的程序。
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.
在code有一个故意的错误在printf语句:
The code has an intentional error in a printf statement:
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
printf("%d\n" , 3.14 ) ;
return 0 ;
}
命令的gcc -Wall hello.c的
给出了一个正确的警告:的hello.c:7:2:警告:格式%d,可预计的说法类型'诠释'... 的
The command gcc -Wall hello.c
gives a correct warning: hello.c:7:2: warning: format '%d' expects argument of type 'int'...
但命令的gcc -std = C99 -Wall hello.c的
不给任何警告。
这两个创建可执行的 A.EXE 的(即运行,并给出了相同的结果)。
Both create an executable a.exe ( that runs and gives the same result ).
(有趣的是命令的gcc -std = gnu99 -Wall hello.c的
给出警告。)
(Interestingly a command gcc -std=gnu99 -Wall hello.c
gives the warning.)
我不知道这是不是一个错误,或者没有安装出错不知怎么的,但似乎都因为编译器不可能作品并成功编译一个较大的项目(但使用时省略,当然同样的警告-std = C99 )。
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 ).
我必须失去了一些信息。
I must be missing some information.
(PS:如果有人有一个新的MinGW的安装,请测试一下)
(ps: If someone has a new MinGW install, please test this.)
gcc版本4.8.1(GCC)
gcc version 4.8.1 (GCC)
的更新1:的
定义 _GNU_SOURCE
包括前 stdio.h中
将警告甚至与的gcc -Wall的hello.c
。
Defining _GNU_SOURCE
before including stdio.h
removes the warning even with gcc -Wall hello.c
.
的更新2(可能不那么重要):的
编译
printf("%lf\n" , 3.14 ) ;
-std = C99
标记输出:0.000000
-std=c99
flag outputs: 0.000000
-std = gnu99
输出:3.140000
-std=gnu99
outputs: 3.140000
和编译:
printf("%f\n" , 3.14 ) ;
-std = gnu99
和 -std = C99
输出:3.140000
-std=gnu99
and -std=c99
output: 3.140000
的更新3:的
这似乎是受影响的功能是:printf的,fprintf中,snprintf的,sprintf的
Functions that seem to be affected are: printf, fprintf, snprintf, sprintf.
推荐答案
使用性病= C99
选项时,与缺乏预警的问题,看起来是因为MinGW的4.8 .1 preprocesses stdio.h中
为的printf()
系列函数时,<$ C有些不同$ C> -std = C99 用于比较时 -std = gnu99
被使用。
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.
请注意:我从TDM看着MinGW的4.8.1 - 我想其他的发行可能会在这些细节上有所不同。
Note: I'm looking at MinGW 4.8.1 from TDM - I think other distributions might differ in these details.
MinGW的已经有一些兼容性问题与格式化因为 MSVCRT.DLL
历史性的依赖C运行时和事实的浮点值的MSVC使用64位再presentation为长双
而GCC使用一个96位(或在x64 128位)再presentation。见<一href=\"http://stackoverflow.com/questions/7134547/gcc-printf-and-long-double-leads-to-wrong-output-c-type-conversion-messes-u\">gcc: printf和长双导致错误的输出。 [C - 类型转换弄乱] 获得一些细节。最近MinGW的版本中已经提供了thier自己实施的printf()
系列函数(以 __的MinGW _
$ P在 libmingwex.a
上的名称$ PFIX)来解决这些问题。
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. See gcc: printf and long double leads to wrong output. [C - Type conversion messes up] for some details. 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.h
和 stdio.h中
配置与否的 libmingwex.a
的实施或 MSVCRT.DLL
的实施将被使用。
The header files _mingw.h
and stdio.h
configure whether or not the libmingwex.a
implementations or the msvcrt.dll
implementations will be used.
看来,如果请求符合ANSI,MinGW的将使用 libmingwex.a
实现(也有一些其他的方式来获得这种配置太 - 看详情头)。接线了printf的用户调用()
到 __ mingw_printf()
在 libmingwex实施。一个
是 stdio.h中
定义做一个静态的内联执行中的printf()
的大约一个电话薄包装到 __ mingw_vfprintf()
。显然, -Wformat
不应用对printf()家庭功能的版本,编译器不相信是库的一部分(一个合理的假设 - 编译器不真正了解这些功能的任何东西)。这个问题可以通过应用相应的功能属性(例如:
__ attribute__((格式(printf的,1,2)))
)是固定的。静态联包装函数
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). 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()
. 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). This problem can be fixed by applying the appropriate function attribute (for example: __attribute__ ((format (printf, 1, 2)))
) to the static inline wrapper functions.
您发现的其他问题,其中的printf(%LF \\ n,3.14)
打印 0.000000
时使用 STD = C99
,看起来是在 libmingwex.a
实施 __ mingw_vfprintf的一个bug ()
。看来, __ mingw_vfprintf()
误间$ P $点%LF
意味着该参数是一个长双
。我不是由太惊讶 - 我总是要查找是否%LF
办法双击
或长双
。
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()
. It seems that __mingw_vfprintf()
mistakenly interprets "%lf"
to mean the argument is a long double
. I'm not too surprised by that - I always have to look up whether %lf
means double
or long double
.
这篇关于MinGW的不产生警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!