2012-05-10 40 views
3

我有一個示例有缺陷的程序應該給出一個有關未初始化變量的警告,但是當我編譯它時,gcc不會給我任何警告。C - 用-Wall編譯並不會警告未初始化的變量

下面是代碼:

#include <stdio.h> 

int main() 
{ 
    int foo; 

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

    return 0; 
} 

這是我跑:cc -Wall testcase.c -o testcase

而且我沒有得到任何反饋。據我所知,這應該產生:

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

這似乎是在His C的教程similar example正確警告肖捷思銳)。這是我第一次嘗試的例子,並注意到它沒有按預期工作。

任何想法?

編輯:

的gcc版本:

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

使用Clang,用它來完成';-)'。 – rubenvb

+0

rubenvb,原來是什麼工作,張貼作爲答案,我會接受它! –

回答

0

使用Clang,完成它。看起來像GCC中的一個bug,因此Clang會像它應該那樣發出警告。

+0

更多信息:更新gcc也是一個選項,但使用clang竟然是我的更好選擇。 –

7

你與優化的編譯打開?這裏是我的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. 

我的gcc版本是:

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

其實,我只是想這對一個gcc 4.4.5,我 GET該警告不使用-O。所以這取決於你的編譯器版本。

+0

感謝您的回答。我試過'cc -Wall -O testcase.c -o testcase'和'cc -Wuninitialized -O testcase.c -o testcase',沒有區別。此外,gcc不會發出有關-Wuninitialized要求-O的警告。 –

+0

@NickKnowlson:看起來在gcc 4.2中,'-Wall'並不意味着'-Wuninitialized'。此外,該版本的gcc似乎無法檢測到「foo」實際上未被使用。 –

+0

http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/Warning-Options.html#Warning-Options文檔以其他方式說明,看起來像。 「該版本的gcc似乎無法檢測到foo實際上未被使用」那麼真正的答案呢?此版本無法檢測到foo由於某種原因未初始化?我會嘗試一些更多的場景,看看我是否可以創造任何條件。 –

2

更新您的編譯器。

$ 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) 
$ 
+4

我可以確認GCC 4.5.3,4.6.3和4.7.0都對此發出警告。 – rubenvb

+0

@rubenvb:您可以(可能)只確認您的發行版在默認配置文件中具有合理的設置,純粹的vanilla gcc甚至不會優化(-O,也不是--march = native,--mtune = native,no graphite沒什麼)。 如果你已經手工編譯了gcc並且可以另外說明,那麼可以使用Mea Culpa。 – AoeAoe

+0

他的問題是使用蘋果dist版本包,llvm是前端也過時(不贊成)afaik。 – AoeAoe

0

一個編譯器不需要由C標準時一個未初始化的變量被訪問的警告。甚至不需要編譯器來警告程序是否調用未定義的行爲(假設沒有語法錯誤並且沒有違反約束)。

使用gcc您可以使用-Wuninitialized爲未初始化的變量啓用警告。正如其他人指出的,在指定-Wall時,最近版本爲gcc-Wuninitialized已啓用。

+0

我很欣賞這種努力,但我已經熟悉了這一切。 :) –

+0

此外,我對此很好奇,看起來'-Wuninitialized'已經在至少自版本2.95.3(2001年3月16日)指定'-Wall'時啓用。所以不要再近了! :) http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_2.html#SEC8 –