C++:GNU G++参数是什么?

5

可能是重复问题:
C/C++编译器的最佳警告级别是什么?

GCC有成千上万个选项可以添加更多警告;我希望-Wall -Wextra -pedantic包含所有有用的选项,但刚才我遇到了-Woverloaded-virtual,这对我来说似乎非常好。

您使用或推荐使用哪些其他G++参数?


也许可以查看文档? - asveikau
2
重复的问题:https://dev59.com/tHRC5IYBdhLWcg3wJNcN。 - EmeryBerger
5个回答

2

虽然不完全属于同一类别,但我总是使用-Werror编译,将警告标记为错误。非常有用。

为了使第三方头文件正常工作,我通过-isystem包含这些头文件,而不是使用-I……否则这些头文件中的警告会破坏构建过程。

还有一个-Weffc++,它会对Meyers的Effective C++中概述的特定问题发出警告。然而,我发现它太严厉了。例如,它会对未声明虚析构函数的基类发出警告。理论上,这很好,但我正在开发一个使用继承进行代码重用(和策略类)的模板库,显然它们没有(也不需要)虚析构函数。


我同意-Weffc++太严格了。不过,偶尔为了检查我的代码,我还是会使用它进行编译。 - maxelost
1
具有讽刺意味的是,-Weffc++警告不要从策略类boost::noncopyable派生,但Effective C++却建议从这样的类继承。同样,它也警告不要从std::unary_function和其伙伴继承。GCC在这里应该更加智能化:一个没有数据成员和公共成员函数的类不太可能被多态地使用。 - Philipp
1
@Philipp:这些问题已经被发现,GCC邮件列表上有几个已提交的错误和讨论……我认为这不会很快得到修复。 - Konrad Rudolph

1
通常我会启用所有警告,然后有选择性地删除那些给出无用输出的标志。在我的一个项目中,我使用以下 C 和 C++ 警告:
-pedantic
-Wall
-Wextra
-Wformat=2
-Wmissing-include-dirs
-Wswitch-default
-Wswitch-enum
-Wunused
-Wstrict-aliasing=1
-Wfloat-equal
-Wundef
-Wunsafe-loop-optimizations
-Wpointer-arith
-Wcast-qual
-Wcast-align
-Wwrite-strings
-Wconversion
-Wmissing-format-attribute
-Wpacked
-Wredundant-decls
-Winvalid-pch
-Wvolatile-register-var
-Wsync-nand
-Wsign-conversion
-Wlogical-op
-Wmissing-declarations
-Wmissing-noreturn
-Wstrict-overflow=5
-Wstack-protector

此外,我使用以下C++标志:

-std=c++98
-Wnon-virtual-dtor
-Wctor-dtor-privacy
-Wstrict-null-sentinel
-Woverloaded-virtual
-Wsign-promo

另外,在发布版本中,我启用了以下警告:

-pedantic-errors
-Werror
-Wuninitialized
-Winit-self
-Wdisabled-optimization

我发现有点烦人的是,-Wall 只启用了绝对最少的警告,而不是像名称所示的“全部”警告。

我本以为在发布模式下使用-Wdisabled-optimization -Werror有点冒险。拒绝任何不适用于所有优化的代码。或者这是一种强制使用小函数的技巧吗?;-) - Steve Jessop
@Steve Jessop:对于那个项目(非常小),它可以工作,但对于其他项目,如果它引起警告,我会禁用该标志。此外,对于大型枚举,-Wswitch-enum可能会变得非常烦人。 - Philipp

1

除了上面已经提到的之外:

-pedantic                   Issue warnings needed for strict compliance to the standard
-Wall
-Wextra                     Print extra (possibly unwanted) warnings
-Werror                     Treat all warnings as errors
-std=c++0x                  Conform to the ISO 1998 C++ standard, with extensions that are likely to be in C++0x 
-std=c++98                  Conform to the ISO 1998 C++ standard  

1

我见过一些被使用的;

-Wcast-qual:每当将指针强制转换以从目标类型中删除类型限定符时,发出警告。例如,如果将const char *强制转换为普通的char *,则会发出警告。

-Wpointer-arith:警告任何取决于函数类型或void大小的内容。GNU C将这些类型分配为1个大小,方便使用void *指针和函数指针进行计算。

-Wwrite-strings:在编译C时,将字符串常量赋予const char[length]类型,以便将其地址复制到非const char *指针中,将得到警告;在编译C++时,警告有关从字符串字面量转换为char *的已弃用的转换方式。默认情况下,此警告对C ++程序启用。这些警告将帮助您在编译时找到可能尝试写入字符串常量的代码,但前提是您在声明和原型中非常小心地使用了const。否则,它只会是一个麻烦事;这就是我们为什么没有使-Wall请求这些警告的原因。

-Wdisabled-optimization:如果禁用了请求的优化传递,则发出警告。这个警告通常并不意味着您的代码有什么问题;它只是表示GCC的优化器无法有效地处理代码。通常,问题在于您的代码太大或太复杂;当优化本身可能需要过多时间时,GCC将拒绝优化程序。


1

请参见C/C++编译器的最佳编译警告级别?。其中一篇帖子包含以下详尽(和令人疲惫)的列表。

   -g -O -Wall -Weffc++ -pedantic  \
    -pedantic-errors -Wextra  -Wall -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

其中一些是多余的。例如,第二个-Wall-Wcomment都被第一个-Wall隐含了。-Werror + -pedantic意味着-pedantic-errors - Steve Jessop
1
确实。无论如何,这是过度设计了。但它是一个很好的起点。 - EmeryBerger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接