简体   繁体   English

C - 使用-Wall编译不会警告未初始化的变量

[英]C - Compiling with -Wall doesn't warn about uninitialized variables

I have an example flawed program that should give exactly one warning about an uninitialized variable, but when I compile it gcc doesn't give me any warnings. 我有一个示例有缺陷的程序,应该给出一个关于未初始化变量的警告,但是当我编译它时,gcc不会给我任何警告。

Here is the code: 这是代码:

#include <stdio.h>

int main()
{
    int foo;

    printf("I am a number: %d \n", foo);

    return 0;
}

Here is what I run: cc -Wall testcase.c -o testcase 这是我运行的: cc -Wall testcase.c -o testcase

And I get no feedback. 我得不到反馈。 As far as I know this should produce: 据我所知,这应该产生:

testcase.c: In function 'main': 
testcase.c:7: warning: 'foo' is used uninitialized in this function

It appears to warn Zed Shaw correctly in a similar example in his C tutorial). 它似乎在他的C教程中以类似的例子正确警告Zed Shaw。 This is the example I had first tried and noticed that it wasn't working as expected. 这是我第一次尝试的例子,并注意到它没有按预期工作。

Any ideas? 有任何想法吗?

EDIT: 编辑:

Version of gcc: gcc的版本:

i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)

Are you compiling with optimisation turned on? 您是否正在编译并启用了优化? Here's what my man gcc page says: 这是我的man gcc页面说的:

  -Wuninitialized Warn if an automatic variable is used without first being initialized or if a variable may be clobbered by a "setjmp" call. These warnings are possible only in optimizing compilation, because they require data flow information that is computed only when optimizing. If you do not specify -O, you will not get these warnings. Instead, GCC will issue a warning about -Wuninitialized requiring -O. 

My version of gcc is: 我的gcc版本是:

i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)

Actually, I just tried this on a gcc 4.4.5 and I do get the warning without using -O . 实际上,我刚刚在gcc 4.4.5上尝试了这个,我确实在不使用-O情况下得到了警告。 So it depends on your compiler version. 所以这取决于你的编译器版本。

Update your compiler. 更新您的编译器。

$ cat test.c
#include <stdio.h>

int main(void)
{
    int foo;
    printf("I am a number: %d \n", foo);
    return 0;
}
$ gcc -Wall -o test ./test.c
./test.c: In function ‘main’:
./test.c:7:11: warning: ‘foo’ is used uninitialized in this function [-Wuninitialized]
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) 
$ 

A compiler is not required by the C Standard to warn when an uninitialized variable is accessed. C标准不要求编译器在访问未初始化的变量时发出警告。 A compiler is even not required to warn if a program invokes undefined behavior (assuming there is no syntax error and no constraint violations). 如果程序调用未定义的行为(假设没有语法错误且没有约束违规),甚至不需要编译器发出警告。

With gcc you can enable warnings for uninitialized variables with -Wuninitialized . 使用gcc您可以使用-Wuninitialized为未初始化的变量启用警告。 As others noted, with recent versions of gcc , -Wuninitialized is enabled when -Wall is specified. 正如其他人所说,使用最新版本的gcc ,在指定-Wall时启用-Wuninitialized

Use Clang , be done with it. 使用Clang ,完成它。 Seems like a bug in GCC, cause Clang warns like it should. 看起来像GCC中的一个错误,因为Clang警告它应该这样。

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

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