2011-10-17 123 views
3

這裏是我的測試程序:爲什麼GCC 4.2.1在某些情況下忽略#pragma GCC診斷忽略「-Wcast-qual」?

#include <stdio.h> 
#pragma GCC diagnostic ignored "-Wcast-qual" 

static void proc(char *buf) 
{ 
    printf("buf=%p\n",buf); 
} 

int main(int argc,char **argv) 
{ 
    const char *cbuf; 
    char *buf = (char *)cbuf; 
    proc(buf); 
    return(0); 
} 

這裏是我的編譯:

$ g++ -Wcast-qual x.cpp 
x.cpp: In function ‘int main(int, char**)’: 
x.cpp:13: warning: cast from type ‘const char*’ to type ‘char*’ casts away constness 
$ 

這裏是沒有-Wcast-QUAL編譯:

$ g++ x.cpp 
$ 

我用#pragma GCC diagnostic ignored在我的代碼的其他地方沒有問題。這裏不起作用。有人可以告訴我爲什麼嗎?

回答

0

這是Mac上的編譯器錯誤。 Linux上的GCC 4.7.2沒有這個問題。鐺++也沒有。在Mac上,你應該嘗試使用clang ++,而不是g ++。

Apple應更新其編譯器。