原文网址:http://blog.sina.com.cn/s/blog_605f5b4f0101bct7.html

New warnings for unused variables and parameters

The behavior of -Wall has changed and now includes the new warning flags -Wunused-but-set-variable and (with -Wall -Wextra-Wunused-but-set-parameter. This may result in new warnings in code that compiled cleanly with previous versions of GCC.

For example,

void fn (void) 
 { 
 int foo; 
 foo = bar ();  
 }

Gives the following diagnostic:

warning: variable "foo" set but not used [-Wunused-but-set-variable]

Although these warnings will not result in compilation failure, often -Wall is used in conjunction with -Werror and as a result, new warnings are turned into new errors.

To fix, first see if the unused variable or parameter can be removed without changing the result or logic of the surrounding code. If not, annotate it with __attribute__((__unused__)).

As a workaround, add -Wno-error=unused-but-set-variable or -Wno-error=unused-but-set-parameter.

-Werror 
     Make all warnings into errors
-Wall 
-Wall turns on the following warning flags:
                 --Waddress   
          -Warray-bounds (only with -O2)  
          -Wc++11-compat  
          -Wchar-subscripts  
          -Wenum-compare (in C/ObjC; this is on by default in C++) 
          -Wimplicit-int (C and Objective-C only) 
          -Wimplicit-function-declaration (C and Objective-C only) 
          -Wcomment  
          -Wformat   
          -Wmain (only for C/ObjC and unless -ffreestanding)  
          -Wmaybe-uninitialized 
          -Wmissing-braces (only for C/ObjC) 
          -Wnonnull  
          -Wparentheses  
          -Wpointer-sign  
          -Wreorder   
          -Wreturn-type  
          -Wsequence-point  
          -Wsign-compare (only in C++)  
          -Wstrict-aliasing  
          -Wstrict-overflow=1  
          -Wswitch  
          -Wtrigraphs  
          -Wuninitialized  
          -Wunknown-pragmas  
          -Wunused-function  
          -Wunused-label     
          -Wunused-value     
          -Wunused-variable  
          -Wvolatile-register-var 
 -Wextra
This enables some extra warning flags that are not enabled by -Wall. (This option used to be called -W. The older name is still supported, but the newer name is more descriptive.)
          -Wclobbered  
          -Wempty-body  
          -Wignored-qualifiers 
          -Wmissing-field-initializers  
          -Wmissing-parameter-type (C only)  
          -Wold-style-declaration (C only)  
          -Woverride-init  
          -Wsign-compare  
          -Wtype-limits  
          -Wuninitialized  
          -Wunused-parameter (only with -Wunused or -Wall) 
      -Wunused-but-set-parameter (only with -Wunused or -Wall)  

Warn whenever a function parameter is assigned to, but otherwise unused (aside from its declaration).
To suppress this warning use the ‘unused’ attribute (see Variable Attributes).
This warning is also enabled by -Wunused together with -Wextra. 

-Wunused-but-set-parameter
Warn whenever a function parameter is assigned to, but otherwise unused (aside from its declaration).
To suppress this warning use the ‘unused’ attribute (see Variable Attributes).
This warning is also enabled by -Wunused together with -Wextra. 
-Wunused-but-set-variable
Warn whenever a local variable is assigned to, but otherwise unused (aside from its declaration). This warning is enabled by -Wall.
To suppress this warning use the ‘unused’ attribute (see Variable Attributes).
This warning is also enabled by -Wunused, which is enabled by -Wall
-Wunused-function
Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall. 
-Wunused-label
Warn whenever a label is declared but not used. This warning is enabled by -Wall.To suppress this warning use the ‘unused’ attribute (see Variable Attributes). 
-Wunused-local-typedefs (C, Objective-C, C++ and Objective-C++ only)
Warn when a typedef locally defined in a function is not used. This warning is enabled by -Wall. 
-Wunused-parameter
Warn whenever a function parameter is unused aside from its declaration.
To suppress this warning use the ‘unused’ attribute (see Variable Attributes). 

6.61.10 Diagnostic Pragmas

https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html

GCC allows the user to selectively enable or disable certain types of diagnostics, and change the kind of the diagnostic. For example, a project's policy might require that all sources compile with -Werror but certain files might have exceptions allowing specific types of warnings. Or, a project might selectively enable diagnostics and treat them as errors depending on which preprocessor macros are defined.

#pragma GCC diagnostic kind option
Modifies the disposition of a diagnostic. Note that not all diagnostics are modifiable; at the moment only warnings (normally controlled by ‘-W...’) can be controlled, and not all of them. Use -fdiagnostics-show-option to determine which diagnostics are controllable and which option controls them.

kind is ‘error’ to treat this diagnostic as an error, ‘warning’ to treat it like a warning (even if -Werror is in effect), or ‘ignored’ if the diagnostic is to be ignored. option is a double quoted string that matches the command-line option.

          #pragma GCC diagnostic warning "-Wformat"
#pragma GCC diagnostic error "-Wformat"
#pragma GCC diagnostic ignored "-Wformat"

Note that these pragmas override any command-line options. GCC keeps track of the location of each pragma, and issues diagnostics according to the state as of that point in the source file. Thus, pragmas occurring after a line do not affect diagnostics caused by that line.

#pragma GCC diagnostic push
#pragma GCC diagnostic pop
Causes GCC to remember the state of the diagnostics as of each push, and restore to that point at each pop. If a pop has no matching push, the command-line options are restored.

          #pragma GCC diagnostic error "-Wuninitialized"
foo(a); /* error is given for this one */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuninitialized"
foo(b); /* no diagnostic for this one */
#pragma GCC diagnostic pop
foo(c); /* error is given for this one */
#pragma GCC diagnostic pop
foo(d); /* depends on command-line options */

GCC also offers a simple mechanism for printing messages during compilation.

#pragma message string
Prints string as a compiler message on compilation. The message is informational only, and is neither a compilation warning nor an error.

          #pragma message "Compiling " __FILE__ "..."

string may be parenthesized, and is printed with location information. For example,

          #define DO_PRAGMA(x) _Pragma (#x)
#define TODO(x) DO_PRAGMA(message ("TODO - " #x)) TODO(Remember to fix this)

prints ‘/tmp/file.c:4: note: #pragma message: TODO - Remember to fix this’.

【转】GCC4.6编译的warning -Werror的更多相关文章

  1. App开发流程之使用分类(Category)和忽略编译警告(Warning)

    Category使得开发过程中,减少了继承的使用,避免子类层级的膨胀.合理使用,可以在不侵入原类代码的基础上,写出漂亮的扩展内容.我更习惯称之为"分类". Category和Ext ...

  2. 将Linux下编译的warning警告信息输出到文件中[整理笔记]

    Linux中,脚本语言环境中,即你用make xxx即其他一些普通linux命令,比如ls,find等,不同的数字,代表不同的含义: 数字 含义 标准叫法0 标准输入  stdin = standar ...

  3. 16种C语言编译警告(Warning)类型的解决方法

    当编译程序发现程序中某个地方有疑问,可能有问题时就会给出一个警告信息.警告信息可能意味着程序中隐含的大错误,也可能确实没有问题.对于警告的正确处理方式应该是:尽可能地消除之.对于编译程序给出的每个警告 ...

  4. 简单编写makefile文件,实现GCC4.9编译项目,增加boost库測试等等。。

    一.须要用到的hw.cpp hw.h funtest.cpp funtest.h makefile 几个測试文件 1.hw.cpp代码例如以下: #include "hw.h" # ...

  5. CMake编译如何解决[-Werror,-Wformat-security] 问题

    在用Android Studio进行Android开发时,常常采用 java代码调用C++代码,即JNI调用native的开发模式. 在上层build.gradle编译脚本里面可以指定C++代码的编译 ...

  6. C语言 消灭编译警告(Warning)

    如何看待编译警告 当编译程序发现程序中某个地方有疑问,可能有问题时就会给出一个警告信息.警告信息可能意味着程序中隐含的大错误,也可能确实没有问题.对于警告的正确处理方式应该是:尽可能地消除之.对于编译 ...

  7. centos5.6部署gcc4.7编译的程序导致问题

    因为用了c++0x的一些新特性,必须使用4.6及以上的版本编译,所以使用了4.7编译,运行时提示错误 libstdc++.so.6(GLIBCXX_3.4.14) 错误 这个时候下了个glibc2.7 ...

  8. 编译出现 WARNING: 'aclocal-1.15' is missing on your system.问题解决

    1. ubuntu14.04 出现这个问题,需要手动安装 Automake-1.15 2. 下载地址: http://ftp.gnu.org/gnu/automake/ http://ftp.gnu. ...

  9. 编译错误:warning C4005]ws2def.h(91): warning C4005: “AF_IPX”: 宏重定义 winsock.h(460) : 参见“AF_IPX”的前一个定义

    [问题] ws2def.h(91): warning C4005: “AF_IPX”: 宏重定义: winsock2.h(460) : 参见“AF_IPX”的前一个定义 [原因] windows.h头 ...

随机推荐

  1. NOI 2013 书法家

    http://uoj.ac/problem/125 我真是日狗了...... 果然还是没有耐心读题,搞到读题读错了2个地方,结果调试了半天...... 言归正传. 动态规划. 这种题目很常见. 我们发 ...

  2. hdu4405:概率dp

    题意: 总共有n+1个格子:0-n 初始情况下在 0号格子 每次通过掷骰子确定前进的格子数 此外 还有一些传送门可以瞬间从 u 点传送到 v 点(必须被传送) 求走到(或超过)n点总共需要掷多少次骰子 ...

  3. CSS的基本认识

    1.定义: 级联样式表(Cascading Style Sheet)简称“CSS”,通常又称为“风格样式表(Style Sheet)”,它是用来进行网页风格设计的. 2.对CSS的基本认识: CSS是 ...

  4. 第32讲 UI组件之 时间日期控件DatePicker和TimePicker

    第32讲 UI组件之 时间日期控件DatePicker和TimePicker 在Android中,时间日期控件相对来说还是比较丰富的.其中, DatePicker用来实现日期输入设置,    Time ...

  5. Django之Cookie与Session

    一.cookie 1.cookie使用 def cookie(request): print(request.COOKIES) # 获取所有的COOKIES obj = render(request, ...

  6. LoadRunner监控windows资源报错Monitor name :Windows Resources. Cannot connect to machine

    目标机:被监控的机器,windows server 2008 R2. 测试机:执行control的机器,windows7 操作:在测试机上执行Control,添加windows的监控 问题现象:Mon ...

  7. 关闭程序 提示 C#

    private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogResult dr = MessageBox ...

  8. Asp.Net中的消息处理---MSMQ系列学习(一)

    刚刚毕业一年,比较浮躁,上次面试被问到消息队列,觉得非常的惭愧因为不知道,所以下定决心一定要学会使用它.以前只是听说过有这么个东西,要说是什么,在什么场景下使用却是无从知晓,因为自己也确实没有在项目中 ...

  9. VS2010使用附加进程的方式调试IIS中的页面介绍

    1.       对要测试的页面设置断点,然后选择”调试”->”附加到进程”,在进程中选择w3wp.exe进程(需要先打开需测试的页面) 2.       配置完成,当打开你需要测试的页面的时候 ...

  10. mac中遇到的mysql编码问题

    由于项目有需要支持表情包输入数据库,自己做了一下技术测试,修改了my.cnf的权限为777.结果就操蛋了.编码错误.... 直到我无意地输入mysql -h,提示我/etc/my.cnf被忽略的一段话 ...