2011-10-17 64 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应更新其编译器。