我有一个 C 程序,其中有几个(尚未)使用的静态函数。我想禁用这些特定功能的警告。我不想禁用所有-Wunused-function
警告。我正在使用 GCC 4.6。具体来说:
gcc --version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
我正在遵循文档中的建议(使用push
and pop
),但我无法让它工作。
我创建了一些简化的源代码来调查这个问题。我正在用gcc -Wall -o pragma pragma.c
(where pragma.c
) 编译它们。我的第一个版本pragma.c
看起来像这样:
void foo(int i) { }
static void bar() { }
int main() { return 0; }
正如预期的那样,我在编译时得到了这个:
pragma.c:3:13: warning: ‘bar’ defined but not used [-Wunused-function]
同样如预期的那样,我可以禁用这样的警告(然后编译成功):
#pragma GCC diagnostic ignored "-Wunused-function"
void foo(int i) { }
static void bar() { }
int main() { return 0; }
但是后来,我尝试了这个:
void foo(int i) { }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
static void bar() { }
#pragma GCC diagnostic pop
int main() { return 0; }
当我编译它时,我得到了原始警告:
pragma.c:4:13: warning: ‘bar’ defined but not used [-Wunused-function]
删除pop
摆脱警告:
void foo(int i) { }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
static void bar() { }
int main() { return 0; }
但我需要一种方法来禁用仅针对特定代码部分的警告。我无法做到这一点。
我很难想象这怎么可能是预期的行为……但是许多其他人已经使用了这个版本的 GCC,如果这是一个错误,它似乎不太可能进入发布版本。
尽管如此,我仍然无法看到这种行为与文档的一致性,该文档说“在一行之后发生的 pragma 不会影响由该行引起的诊断。”
我究竟做错了什么?是否有关于问题的更多信息,例如错误报告和/或有关可能的解决方法的信息?