我们如何禁用 C 预处理中的特定警告

问题描述 投票:0回答:1

我已为 C 预处理命令启用

-Werror
,以将警告视为错误。 基本上我想将
only
对待
redefined
警告视为错误。所有其他警告不应该被视为错误。

In file included from /home/test/version24/test.spec:35:0, /test/release/base_version.spec:93:0: warning: "CD_MK_FILE" redefined #define CD_MK_FILE 4 In file included from /home/test/version23/mss_litter.spec:19:0, from /test/release/base_version23.spec:0: note: this is the location of the previous definition #define CD_MK_FILE 3
是否有任何标志仅将重新定义的警告视为错误。
谢谢!

c makefile
1个回答
2
投票
快速总结

答案是

-Werror=macro-redefined

。这只会将重新定义的警告视为错误,所有其他警告将不会被视为错误。

详细信息:我们如何启用或禁用特定的编译器警告或错误?

对于

gcc

/
g++
clang
 编译器,请尝试:

# To disable -Werror just for warning -Wwhatever -Wno-error=whatever # To **enable** errors just for this one -Wwhatever warning -Werror=whatever
对于 clang 编译器,您可以使用 

-Wno-error=macro-redefined

disable 仅针对 -Wmacro-redefined
 警告的错误,并使用 
-Werror=macro-redefined
enable only 该警告的错误。

请参阅此处:

https://clang.llvm.org/docs/DiagnosticsReference.html#wmacro-redefine。感谢您在问题下的评论,@user3386109

请在此处查看所有可能的警告和错误的列表:

    对于 gcc 编译器:
  1. https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  2. 对于 clang 编译器(根据设计与 gcc 兼容):
  3. https://clang.llvm.org/docs/DiagnosticsReference.html
例如,来自上面的 gcc 链接:

-Werror=

将指定的警告变为错误。附加警告说明符;例如

-Werror=switch

 将由 
-Wswitch
 控制的警告转变为错误。此开关采用否定形式,用于否定特定警告的 
-Werror
;例如,即使 
-Wno-error=switch
 有效,
-Wswitch
 也不会导致 
-Werror
 警告成为错误。

每个可控警告的警告消息包括控制警告的选项。然后,该选项可以与

-Werror=

-Wno-error=
 一起使用,如上所述。 (可以使用 
-fno-diagnostics-show-option
 标志禁用警告消息中选项的打印。)

请注意,指定

-Werror=foo

 自动意味着 
-Wfoo
。然而,
-Wno-error=foo
并不意味着任何事情。

其他参考资料:

    @HolyBlackCat 的
  1. 评论
  2. [我的问答]
  3. 如何禁用 Bazel 中的 C/C++ -Werror
     构建错误? (又名:如何关闭已由 
    -Wall -Werror
     打开的特定警告)
相关:

    如果您只需要控制几行代码,这也非常有用:
  1. 如何禁用几行代码的 GCC 警告
© www.soinside.com 2019 - 2024. All rights reserved.