2009-07-03 79 views
36

在Microsoft編譯器上,可以使用#pragma禁用特定的警告,而不禁用其他警告。如果編譯器警告「必須完成」的事情,這是非常有用的功能。禁用gcc中的特定警告

GCC在這一點上是否有類似的功能?這似乎是一個足夠明顯的特徵,它不可能想到它還沒有這個功能,但是網絡上的舊信息表明這個特徵不存在。

在GCC中使用什麼?

具體來說,我喜歡使用多字符常量,如'abc'。這些數據可以有效地評估爲基本256位數 - 這是一個非常方便的功能,但會觸發警告。它非常方便在case語句中切換四個字符串。

+3

重複:http://stackoverflow.com/questions/487108/how-to-supress-specific-warnings-in-g http://stackoverflow.com/問題/ 925179 /選擇性刪除警告http://stackoverflow.com/questions/965093/selectively-disable-gcc-warnings-for-only-part-of-a-translation-unit – 2009-07-04 07:59:58

回答

33

這可以用gcc的diagnostic pragmas完成。

+0

這將是有用的,一旦我升級到更新的GCC。它似乎是一個更新的功能。 – 2009-07-03 17:38:41

24

從GCC手冊:

Many options have long names starting with -f or with -W---for example, 
    -fforce-mem, -fstrength-reduce, -Wformat and so on. Most of these have 
    both positive and negative forms; the negative form of -ffoo would be 
    -fno-foo. This manual documents only one of these two forms, whichever 
    one is not the default. 

但如果你問是否有一個源級別的警告禁用,如果該功能在GCC存在我不知道。

11

-Wno-multichar

如果多字符常量( 'FOOF')使用時不發出警告。通常,它們表示 中的錯字,因爲它們具有實現定義的值,不應在 便攜式代碼中使用。

More information

7

內源代碼寫:

#pragma GCC diagnostic ignored "-Wno-multichar" 

// code with warnings but wont be displayed now... 
+0

另外值得注意的是,可以使用'#pragma GCC診斷推送'和'#pragma GCC診斷流行'來爲小部分代碼設置診斷。 – Hector 2017-11-30 03:27:13