C/C++ 编译器的最佳编译器警告级别? [英] Best compiler warning level for C/C++ compilers?

查看:96
本文介绍了C/C++ 编译器的最佳编译器警告级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于不同的 C/C++ 编译器,您推荐什么编译器警告级别?

gcc 和 g++ 将让您在默认级别上摆脱很多.我发现对我来说最好的警告级别是-Wall".而且我总是尝试删除修复它生成的警告的代码.(即使是关于将括号用于逻辑优先规则或说我的意思是如果(x = y)"的愚蠢做法)

您最喜欢 Sun CC、aCC (HPUX ?)、Visual Studio、intel 等不同编译器的哪些级别?

我只是想指出我不在 gcc/g++ 上使用-Werror"(但我确实理解它的实用程序),因为我使用:

<上一页>#warning 这是给我自己的便条"

在我的代码中的几个地方.所有的编译器都理解#warning 宏吗?

解决方案

这是我用于 C++ 代码的一组超偏执标志:

 -g -O -Wall -Weffc++ -pedantic -pedantic-errors -Wextra -Waggregate-return -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment -Wconversion -W禁用优化-Werror -Wfloat-equal -Wformat -Wformat=2 -Wformat-非文字 -Wformat-security -W格式-y2k -Wimplicit -Wimport -Winit-self -Winline -Winvalid-pch -Wunsafe-loop-optimizations -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wpadded -W括号 -Wpointer-arith -Wredundant-decls -Wreturn 类型 -Wsequence-point -Wshadow -Wsign-compare -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch -Wswitch-default -Wswitch-enum -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunreachable-code -Wunused -Wunused-function -Wunused-label -Wunused-参数-Wunused 值 -Wunused 变量 -Wvariadic 宏 -Wvolatile-register-var -Wwrite-strings

这应该会给你一些开始的东西.根据项目的不同,您可能需要将其调低,以免看到来自第三方库的警告(这些库通常对无警告非常粗心.)例如,Boost 向量/矩阵代码会使 g++ 发出很多噪音.

处理这种情况的一种更好的方法是围绕 g++ 编写一个包装器,该包装器仍然使用调整到最大的警告,但允许人们禁止在特定文件/行号中看到它们.很久以前就写过这么一个工具,等有时间清理一下再发布.

What compiler warning level do you recommend for different C/C++ compilers?

gcc and g++ will let you get away with a lot on the default level. I find the best warning level for me is '-Wall'. And I always try to remove fix the code for the warnings it generates. (Even the silly ones about using parenthesis for logical precedence rules or to say I really mean 'if (x = y)')

What are your favorite levels for the different compilers, such as Sun CC, aCC (HPUX ?), Visual Studio, intel?

Edit:

I just wanted to point out that I don't use "-Werror" (but I do understand it's utility) on gcc/g++ because, I use:

#warning "this is a note to myself"

in a few places in my code. Do all the compilers understand the #warning macro?

解决方案

This is a set of extra-paranoid flags I'm using for C++ code:

    -g -O -Wall -Weffc++ -pedantic  
    -pedantic-errors -Wextra -Waggregate-return -Wcast-align 
    -Wcast-qual  -Wchar-subscripts  -Wcomment -Wconversion 
    -Wdisabled-optimization 
    -Werror -Wfloat-equal  -Wformat  -Wformat=2 
    -Wformat-nonliteral -Wformat-security  
    -Wformat-y2k 
    -Wimplicit  -Wimport  -Winit-self  -Winline 
    -Winvalid-pch   
    -Wunsafe-loop-optimizations  -Wlong-long -Wmissing-braces 
    -Wmissing-field-initializers -Wmissing-format-attribute   
    -Wmissing-include-dirs -Wmissing-noreturn 
    -Wpacked  -Wpadded -Wparentheses  -Wpointer-arith 
    -Wredundant-decls -Wreturn-type 
    -Wsequence-point  -Wshadow -Wsign-compare  -Wstack-protector 
    -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch  -Wswitch-default 
    -Wswitch-enum -Wtrigraphs  -Wuninitialized 
    -Wunknown-pragmas  -Wunreachable-code -Wunused 
    -Wunused-function  -Wunused-label  -Wunused-parameter 
    -Wunused-value  -Wunused-variable  -Wvariadic-macros 
    -Wvolatile-register-var  -Wwrite-strings

That should give you something to get started. Depending on a project, you might need to tone it down in order to not see warning coming from third-party libraries (which are usually pretty careless about being warning free.) For example, Boost vector/matrix code will make g++ emit a lot of noise.

A better way to handle such cases is to write a wrapper around g++ that still uses warnings tuned up to max but allows one to suppress them from being seen for specific files/line numbers. I wrote such a tool long time ago and will release it once I have time to clean it up.

这篇关于C/C++ 编译器的最佳编译器警告级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆