这个警告来自gcc中名为"cccp.c"的文件(截至2.95版本;这个文件来自“苏联俄罗斯”吗?),它无法关闭。目前即使在git head中仍然没有选项可以单独禁用此警告,gcc/libcpp/macro.c 文件 (同一文件的第2527行和第2994行)。
我稍微引用了一些来源。
2525
2526 static bool
2527 warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
2528 const cpp_macro *macro2)
2529 {
...
2537
..
2545
...
2550
...
2561
...
2566
...
2573 for (i = 0; i < macro1->count; i++)
2574 if (!_cpp_equiv_tokens (¯o1->exp.tokens[i], ¯o2->exp.tokens[i]))
2575 return true;
在你的情况下,warn_of_redefinition
函数将返回true。这是一个真实的用法示例:
2989 if (node->type == NT_MACRO)
2990 {
2991 if (CPP_OPTION (pfile, warn_unused_macros))
2992 _cpp_warn_if_unused_macro (pfile, node, NULL);
2993
2994 if (warn_of_redefinition (pfile, node, macro))
2995 {
2996 const int reason = (node->flags & NODE_BUILTIN)
2997 ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
2998 bool warned;
2999
3000 warned = cpp_pedwarning_with_line (pfile, reason,
3001 pfile->directive_line, 0,
3002 "\"%s\" redefined",
3003 NODE_NAME (node));
3004
3005 if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
3006 cpp_error_with_line (pfile, CPP_DL_NOTE,
3007 node->value.macro->line, 0,
3008 "this is the location of the previous definition");
3009 }
3010 }
所以,没有任何具体的选项。Greg的回答对于这种情况很好,只需要在重新定义之前取消定义你的空字符串即可。
-Wp,-w
来禁用预处理器警告。 - osgx