除了最后一个,所有的#pragma GCC诊断都被忽略

除了最后一个,所有的#pragma GCC诊断都被忽略,c,gcc,pragma,C,Gcc,Pragma,使用#pragma GCC诊断推送/弹出/警告/忽略时。。。似乎只有最后的#pragma-行生效!为什么? 作为一个例子,我从中复制并修改了gcc 7.3.0的示例 使用最后一个#pragma,因为foo和bar放在代码中所有pragma行之后。试试这个: #pragma GCC diagnostic warning "-Wunused-variable" static void foo() { volatile uint32_t testArray[UINT8_MAX] = {0};

使用
#pragma GCC诊断推送/弹出/警告/忽略时
。。。似乎只有最后的
#pragma
-行生效!为什么? 作为一个例子,我从中复制并修改了gcc 7.3.0的示例

使用最后一个
#pragma
,因为
foo
bar
放在代码中所有pragma行之后。试试这个:

#pragma GCC diagnostic warning "-Wunused-variable"

static void foo() {
    volatile uint32_t testArray[UINT8_MAX] = {0};
}
pragma
会影响此行之后的代码,不会像您预期的那样跟随函数调用。

您需要在编译的代码周围设置#pragma行,在本例中是foo和bar函数,而不是它们的调用。编译器不执行代码,所以将#pragma行放在它们应该放的地方。
#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 GCC diagnostic warning "-Wunused-variable"

static void foo() {
    volatile uint32_t testArray[UINT8_MAX] = {0};
}