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

发布于 2024-10-11 08:20:15 字数 390 浏览 4 评论 0原文

可能的重复:
C/C++ 编译器的最佳编译器警告级别?

GCC 有数千个添加更多警告的选项;我希望 -Wall -Wextra -pedantic 包含所有有用的内容,但现在我遇到了 -Woverloaded-virtual< /code> 这对我来说真的很好。

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

Possible Duplicate:
Best compiler warning level for C/C++ compilers?

GCC has thousands of options to add more warnings; I was hoping that -Wall -Wextra -pedantic included all the useful ones, but just now I met -Woverloaded-virtual which seems really nice to me.

What other G++ parameters do you use or would you recommend?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风吹过旳痕迹 2024-10-18 08:20:16

不完全相同的类别,但我总是使用 -Werror 进行编译,将警告标记为错误。非常有用。

为了使此功能与第 3 方标头一起工作,我通过 -isystem 而不是 -I 包含这些标头……否则这些标头中的警告将破坏构建。

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

Not quite the same category but I always compile with -Werror to flag warnings as errors. Very useful.

To make this work with 3rd party headers, I include those headers via -isystem instead of -I … otherwise warnings in those headers will break the build.

There’s also -Weffc++ which warns for specific issues outlined in Meyers’ Effective C++. However, I’ve found this too harsh. For example, it warns for base classes that don’t declare virtual destructors. In theory, this is very nice but I’m working on a template library that uses inheritance for code reuse (and policy classes) and obviously they don’t have (nor need) virtual destructors.

不知在何时 2024-10-18 08:20:16

请参阅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

See Best compiler warning level for C/C++ compilers?. One post contains the following exhaustive (and exhausting) list.

   -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
坠似风落 2024-10-18 08:20:16

有些是我见过的,已经被使用过;

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

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

-Wwrite-strings:编译 C 时,将字符串常量指定为 const char[length] 类型,以便将 1 的地址复制到非 const char * 指针会收到警告;编译 C++ 时,警告不建议使用从字符串文字到 char * 的转换。默认情况下,对于 C++ 程序启用此警告。这些警告将帮助您在编译时找到可以尝试写入字符串常量的代码,但前提是您在声明和原型中非常小心地使用 const 。否则,只会带来麻烦;这就是为什么我们没有让 -Wall 请求这些警告。

-Wdisabled-optimization:如果请求的优化过程被禁用,则发出警告。此警告通常并不表明您的代码有任何问题;它仅仅表明 GCC 的优化器无法有效地处理代码。通常,问题是您的代码太大或太复杂;当优化本身可能花费大量时间时,GCC 将拒绝优化程序。

Some that I've seen that are used;

-Wcast-qual: Warn whenever a pointer is cast so as to remove a type qualifier from the target type. For example, warn if a const char * is cast to an ordinary char *.

-Wpointer-arith: Warn about anything that depends on the size of a function type or of void. GNU C assigns these types a size of 1, for convenience in calculations with void * pointers and pointers to functions.

-Wwrite-strings: When compiling C, give string constants the type const char[length] so that copying the address of one into a non-const char * pointer will get a warning; when compiling C++, warn about the deprecated conversion from string literals to char *. This warning, by default, is enabled for C++ programs. These warnings will help you find at compile time code that can try to write into a string constant, but only if you have been very careful about using const in declarations and prototypes. Otherwise, it will just be a nuisance; this is why we did not make -Wall request these warnings.

-Wdisabled-optimization: Warn if a requested optimization pass is disabled. This warning does not generally indicate that there is anything wrong with your code; it merely indicates that GCC's optimizers were unable to handle the code effectively. Often, the problem is that your code is too big or too complex; GCC will refuse to optimize programs when the optimization itself is likely to take inordinate amounts of time.

怀念你的温柔 2024-10-18 08:20:16

一般来说,我启用所有警告,然后有选择地删除那些提供无用输出的标志。在我的一个项目中,我使用以下 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< /code> 仅启用绝对最少的警告,而不是“全部”,顾名思义。

In general I enable all warnings and then remove those flags selectively that give useless outputs. In one of my projects, I use the following C and C++ warnings:

-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

In addition, I use the following C++ flags:

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

In addition, for the release build I enable the following warnings:

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

I find it quite annoying that -Wall enables only the absolute minimum of warnings instead of "all", as the name implies.

不一样的天空 2024-10-18 08:20:16

除了上面已经提到的:

-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  

In addition to the ones already mentioned above:

-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  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文