c - 除了最后一个之外,所有#pragma GCC 诊断都被忽略
问题描述
使用#pragma GCC diagnostic push/pop/warning/ignored
...时似乎只有最后#pragma
-行生效!为什么?作为示例,我从此处复制并修改了为 gcc 7.3.0 给出的示例
#include <stdio.h>
#include <stdint.h>
static void foo();
static void bar();
static void car();
static void dar();
int main() {
#pragma GCC diagnostic warning "-Wunused-variable"
foo(); /* error is given for this one */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
bar(); /* no diagnostic for this one */
#pragma GCC diagnostic pop
car(); /* error is given for this one */
#pragma GCC diagnostic pop
dar(); /* depends on command line options */
return 0;
}
static void foo() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
static void bar() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
static void car() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
static void dar() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
用两种方式编译上面的代码
-Wall
在命令行中添加-Wall
在命令行中省略
将导致 1. 对所有对 、 和 的调用发出警告foo()
,而bar()
2.不会对任何发出警告 .. 表明最后一个是唯一生效的,并且是遵循命令行规则的那个. 当然,我不是仅通过这个例子得出这个结论,而是我在这里代表的那个。car()
dar()
#pragma GCC diagnostic pop
关于为什么会这样的任何想法?我做错了吗?
编辑:接受的答案导致以下工作代码:
#include <stdio.h>
#include <stdint.h>
static void foo();
static void bar();
static void car();
static void dar();
int main() {
foo(); /* error is given for this one */
bar(); /* no diagnostic for this one */
car(); /* error is given for this one */
dar(); /* depends on command line options */
return 0;
}
#pragma GCC diagnostic warning "-Wunused-variable"
static void foo() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
static void bar() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
#pragma GCC diagnostic pop
static void car() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
#pragma GCC diagnostic pop
static void dar() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
解决方案
使用最后一个#pragma
,因为foo
和bar
被放置在您的代码中所有 pragma 行之后。尝试这个:
#pragma GCC diagnostic warning "-Wunused-variable"
static void foo() {
volatile uint32_t testArray[UINT8_MAX] = {0};
}
pragma
影响这一行之后的代码,并且不会像您期望的那样遵循函数调用。
推荐阅读
- react-native - 如何证明并排按钮以它们之间的间距居中?
- azure - Azure 物联网中心零接触预配
- c# - Unity 如何为每个特定级别保存高分?
- android - 没有为 FloatingActionButton.Behavior 调用 onStartNestedScroll 方法
- jenkins - 无法在 Jenkinsfile 脚本中使用带有闭包的 Groovy 集合
- data-structures - 从自相交多边形中裁剪闭合多边形的算法
- java - Java eclipse PAHO 验证发布是否成功
- javascript - 在 cubejs 中构建正确的模式 sql
- python - 读取数据集的前几列和行而不是整个数据集
- typescript - 打字稿错误:“一个类只能实现一个对象类型或对象类型与静态已知成员的交集。” 实施部分时