簡體   English   中英

我們如何禁用來自 C 預處理的特定警告

[英]How do we disable specific warnings from C preprocessing

我為 C 預處理命令啟用了-Werror以將警告視為錯誤。 基本上我onlyredefined的警告視為錯誤。 所有其他警告不應視為錯誤。

  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

是否有任何標志僅將重新定義的警告視為錯誤。 謝謝!

我們如何啟用或禁用特定的編譯器警告或錯誤?

對於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僅針對-Wmacro-redefined警告禁用該錯誤,並且您將使用-Werror=macro-redefined僅為該警告啟用錯誤.

請參閱此處: https://clang.llvm.org/docs/DiagnosticsReference.html#wmacro-redefined 感謝@user3386109問題下的評論!

請在此處查看所有可能的警告和錯誤的列表:

  1. 對於 gcc 編譯器: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  2. 對於 clang 編譯器(與 gcc 兼容,設計): 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並不意味着任何東西。

其他參考:

  1. @HolyBlackCat 的評論
  2. [我的問答]如何禁用 Bazel 中的 C/C++ -Werror構建錯誤? (又名:如何關閉已由-Wall -Werror打開的特定警告)

有關的:

  1. 如果您只需要控制幾行代碼,這也非常有用: How to disable GCC warnings for a few lines of code

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM