gcc:使用 -Werror 和 -pedantic 被认为是好的做法吗?
我只是在深入研究 gcc 手册,有些事情对我来说仍然不清楚:
- 当指定 std 时,我应该始终结合使用 -pedantic 吗?
- 当使用 -g 时,标准级别就足够了,或者我应该指定级别 3,即 -g3?
- 使用 -Werror 将所有警告提升为错误,使用 -pedantic-errors 将所有迂腐警告提升为错误,这是一个好习惯吗?
I'm just digging into the gcc manual and some things are still unclear to me:
- When specifying a std, should I always use -pedantic in conjunction?
- When using -g, it the standard level sufficient or should I specify level 3, i.e.
-g3? - Is it good practice to use -Werror to promote all warnings to errors and -pedantic-errors to promote all pedantic warnings to errors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您正在编写一个库,请务必确保像这样的简单程序
在编译时不会出现任何警告,即使编译器在最迂腐的模式下运行并启用了所有可选警告。
如果您正在编写应用程序,那么您的代码左右抛出警告只是您的应用程序的问题。然而,对于库的公共头文件,以后使用该库的每个人都将被迫忽略或忍受头文件引起的警告。
因此,如果可能在多种编译器模式下,请检查您的库标头是否在没有警告的情况下进行编译:
编译您的库抛出多少警告又只是您的问题。
If you are writing a library, please do make sure that a simple program like
compiles without any warnings whatsoever even when the compiler is running in the most pedantic mode with all optional warnings enabled.
If you are writing an application, your code throwing warnings left and right is just your application's problem. With a library's public header file, however, everybody who later uses that library will be forced to ignore or endure the warnings your header file is causing.
So please check that your library headers compile without warnings, if possible in multiple compiler modes:
How many warnings compiling your library throws is again only your problem.
这些都是非常主观的。以下是我的观点:
您对
-pedantic
的使用不应与-std=foo
的使用联系在一起。如果您想要迂腐的错误消息,请使用-pedantic
;如果您使用-pedantic
,您可能还需要-Wall
和-Wextra
,因为您的目标通常是捕获所有可能的失误,无论多么轻微。请注意,-pedantic
不会捕获所有可能的警告,只会捕获 ISO C 标准需要诊断的警告。我一直发现级别 2(默认为
-g
)足以满足我的目的。第 3 级还包括有关程序中所有宏定义的信息,但这仅在调试器支持宏扩展时才有用;我的没有(GNU gdb 6.3.50-20050815(Apple 版本 gdb-696))。我不知道 3 级还包含哪些 2 级不包含的内容。这要看情况。如果您的目标是制作最可移植、最符合标准的代码,那么是的,我强烈建议始终使用
-Werror
和-pedantic-error
(以及-Wall
和-Wextra
),尤其是在开始新项目时。但是,如果您从大型代码库开始,打开这些选项可能会给您带来大量无害的虚假错误,特别是对于有符号/无符号不匹配以及各种类型之间的隐式转换之类的情况。您将花费很长时间来修复代码库以消除这些错误,因此我不推荐它。如果您正在做一个快速的一次性项目,请不要打扰,因为这些只会减慢您的速度。
如果您正在开发一个库或一些开源的东西,那么请打开它们。您的用户将非常感激您的代码不会产生错误或警告。另请考虑 ndim 的建议 .
These are all highly subjective. Here are my opinions:
Your use of
-pedantic
shouldn't be tied to your use of-std=foo
. Use-pedantic
if you want pedantic errors messages; if you're using-pedantic
, you'll probably also want-Wall
and-Wextra
, since your goal is usually to catch all possible missteps, no matter how minor. Note that-pedantic
will not catch all possible warnings, only the warnings that the ISO C standard requires a diagnostic for.I've always found level 2 (the default with
-g
) to be good enough for my purposes. Level 3 also includes information about all macro definitions in your program, but that's only useful if your debugger supports macro expansions; mine does not (GNU gdb 6.3.50-20050815 (Apple version gdb-696)). I don't know what else level 3 includes that level 2 does not.That depends. If your goal is to make the most portable, most standards-compliant code, then yes, I'd highly recommend always using
-Werror
and-pedantic-error
(along with-Wall
and-Wextra
), especially when starting new projects. However, if you're starting with a large codebase, turning on these options will likely give you tons of harmless, spurious errors, especially for things like signed/unsigned mismatch and implicit conversions between various types. It will take you a very long time to fix up a codebase to remove these errors, so I wouldn't recommend it.If you're doing a quick throwaway project, don't bother, since these will only slow you down.
If you're working on a library or something that will get open sourced, then please do turn these on. Your users will greatly appreciate the fact that your code does not produce errors or warnings. Also take ndim's advice into account.
所有这些都是意见问题和/或取决于您的代码库和编程实践。我总是使用
-Wall
和-pedantic
进行编译,我不关心事情是否被标记为错误或警告,所以我从不使用-Werror
,而且我很少使用调试器调试代码 - 当我这样做时,-g
就足够了。其他人可能会给出完全不同的答案。All of these are matters of opinion and/or dependent on your codebase and programming practices. I always compile with
-Wall
and-pedantic
, I don't care whether things are flagged as errors or warnings, so I never use-Werror
, and I very rarely debug code using the debugger - when I do,-g
suffices. Someone else might come up with a completely different answer.我喜欢对 C++ 使用 -Wall -Wextra。但是,如果 -Wextra 报告我使用的库标头的警告,我通常会放弃它。
I like to use -Wall -Wextra for C++. However, if -Wextra reports warnings on headers of libraries I use I usually drop it.