GCC - 不应该警告发出? [英] GCC - shouldn't a warning be issued?

查看:106
本文介绍了GCC - 不应该警告发出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近成立了一个MinGW的MSYS +环境中的我的笔记本电脑来检查的事情是如何使用Netbeans C / C ++的支持。一切似乎都很好地工作,但是,我的测试过程中,我注意到GCC和微软的编译器的cl.exe之间的差异。

I've recently set up a MinGW + MSYS environment on my laptop to check how things are with Netbeans C/C++ support. Everything seems to work fine, however, during my testing I have noticed a difference between GCC and Microsoft's cl.exe compiler.

下面是一个示例程序:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main(void) {
    int i_max = INT_MAX;
    char c_max = CHAR_MAX, c;

    c = i_max;
    printf("i_max: %d, c_max: %d, c: %d\n", i_max, c_max, c);
    return EXIT_SUCCESS;
}

输出是:

i_max: 2147483647, c_max: 127, c: -1

正如你可以在上面的code看到,我分配一个int为char。不应该这样产生可能发生可能导致数据丢失的警告?微软的编译器(我已经配置是非常严格)不发出警告的同时GCC没有。

As you can see in the code above, I assign an int to a char. Shouldn't this produce a warning that a possible data loss may occur? Microsoft's compiler (which I have configured to be very strict) does issue the warning while GCC doesn't.

下面是我使用GCC选项:

Here are the GCC options I use:

-g -Werror -ansi -pedantic -Wall -Wextra

我缺少一些GCC选项进行编译时检查更严格?

Am I missing some GCC option to make the compile time checks even stricter?

推荐答案

您正在寻找

-Wconversion

您得问一个gcc开发商为什么一些警告,不包含在 -Wall -Wextra

You'd have to ask a gcc developer for the specific reasons why some warnings aren't included in -Wall or -Wextra.

不管怎样,这些都是我用的标志:

Anyway, these are the flags I use:

-Wall -Wextra -Wmissing-prototypes -Wmissing-declarations -Wshadow
-Wpointer-arith -Wcast-align -Wwrite-strings -Wredundant-decls -Wnested-externs
-Winline -Wno-long-long -Wconversion -Wstrict-prototypes

至于其他的人士指出, -Wconversion 的行为用的 4.3版 - 老警告有关原型强制类型转换,现在可作为 -Wtraditional转换

As other's have pointed out, the behaviour of -Wconversion changed with version 4.3 - the old warning about prototypes forcing a type conversion is now available as -Wtraditional-conversion.

这篇关于GCC - 不应该警告发出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆