暂时禁用重新定义时的gcc警告

暂时禁用重新定义时的gcc警告,gcc,c-preprocessor,Gcc,C Preprocessor,我正在努力使这项工作(在GCC4.6)没有对我吠叫 #define FOO "" #define BAR "" #if .... #define FOO "Foo, good sir" #endif #if ... #define BAR "Bar, my lady" #endif .... #define EVERYTHING FOO BAR ... 我将有很多这样的。这样做,而不是: #if ... #define FOO "F

我正在努力使这项工作(在GCC4.6)没有对我吠叫

#define FOO  ""
#define BAR  ""

#if ....
    #define FOO    "Foo, good sir"
#endif

#if ...
    #define BAR    "Bar, my lady"
#endif
....

#define EVERYTHING      FOO BAR ...
我将有很多这样的。这样做,而不是:

#if ...
    #define FOO    "Foo"
#else
    #define FOO    ""
#endif
节省大量代码,并使其更具可读性。我得到的警告是:

警告:“FOO”已重新定义[默认启用]

是否有办法在该特定部分的代码中禁用此警告?我发现要禁用某些警告,但我无法在此处找到需要禁用的警告(在此列表中)

有人知道怎么做吗?或者使用另一种方法来避免将它们全部定义为空字符串?

尝试使用
\undef

#define FOO ""

#if ....
    #undef FOO
    #define FOO "Foo, good sir"
#endif

此警告来自gcc中名为“cccp.c”的文件(从2.95版本开始;此文件是否来自“苏联”?),无法关闭。即使在git head中,仍然没有单独禁用此警告的选项(第2527行和同一文件的第2527行)

我会引用一些消息来源

2525 /* Returns nonzero if a macro redefinition warning is required.  */
2526 static bool
2527 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
2528                       const cpp_macro *macro2)
2529 {
...
2537   /* Suppress warnings for builtins that lack the NODE_WARN flag.  */
..
2545   /* Redefinitions of conditional (context-sensitive) macros, on
2546      the other hand, must be allowed silently.  */
...
2550   /* Redefinition of a macro is allowed if and only if the old and new
2551      definitions are the same.  (6.10.3 paragraph 2).  */
...
2561   /* Check parameter spellings.  */
...
2566   /* Check the replacement text or tokens.  */
...
2573   for (i = 0; i < macro1->count; i++)
2574     if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
2575       return true;
因此,没有任何具体的选择。Greg的回答对这种情况很好,只需在重新定义之前取消空字符串的定义。

或者尝试使用if-else

#if ...
#   define FOO "Foo, doof sir"
#else
#   define FOO ""
#endif

您可以使用
-Wp,-w
禁用此警告,该警告来自gcc中名为“cccp.c”的苏俄文件(从2.95版本起),并且无法关闭。仍然没有选项单独禁用此警告(以及同一文件的第2994行)@osgx如果您将该评论作为答案,我将接受它。谢谢您,随机下载投票者!我很高兴我3.5年的问题没有让你满意!在苏俄,宏重新定义了你!
#if ...
#   define FOO "Foo, doof sir"
#else
#   define FOO ""
#endif