c++ - C/C++ 编译器的最佳编译器警告级别?

标签 c++ c warnings compiler-construction

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

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

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

编辑:

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

#warning "this is a note to myself"

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

最佳答案

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

    -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

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

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

关于c++ - C/C++ 编译器的最佳编译器警告级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/399850/

相关文章:

c++ - 是否有将 REFIID 转换为有用名称的操作系统功能?

c++ - 使用open cv和visual studio进行人脸识别

c++ - 为什么在大多数平台上没有 'aligned_realloc'?

c - 通过数组的最有效方法?

xcode - 警告 : Scene is unreachable due to lack of entry point and does not have an identifier for runtime access. swift

c++ - GCC 优化 : how can less operations be slower?

c++ - 非 IEEE 754 浮点类型的大小如何受到限制?

c - 合并排序未显示 C 中的所有答案

c++ - 模板参数初始化

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP