-I编译器选项如何引起不同的警告触发器?

问题描述 投票:2回答:1

我最近受命重组应用程序,并遇到了一个非常奇怪的问题。我已尽可能将问题简化为示例代码。考虑以下内容:

offender.h

__attribute__ ((visibility ("default")))
typedef struct A {
    int a;
} A;

test.cpp:

#include <iostream>

#include <offender.h>

int main(void)
{
    A a;
    a.a = 10;
    std::cout << a.a << "\n";
    return 0;
}

如果我跑步:

g++ -Wall -Werror -o test -I. test.cpp
In file included from test.cpp:3:
./offender.h:4:3: error: ‘visibility’ attribute ignored [-Werror=attributes]
    4 | } A;
      |   ^
cc1plus: all warnings being treated as errors
make: *** [Makefile:3: all] Error 1

但是,如果我将offender.h移至系统定义的路径并在不使用-I的情况下进行编译,这是有道理的。我明白了:

sudo mv offender.h /usr/local/include/
g++ -Wall -Werror -o test test.cpp

未触发警告。

如果我从当前目录中包含头文件,但如果从预定义的包含目录中包含头文件,则可能会发生警告?我想念什么?

这已经在g ++ 7.5.0(Ubuntu 18.04)和9.3.0(Ubuntu 20.04)上进行了测试,它们都生成相同的输出。

编辑:澄清了问题

c++ g++ compiler-warnings
1个回答
4
投票

documentation

声明与操作系统和运行时库的接口的头文件通常无法严格按照C编写。因此,GCC对在系统头中找到的代码给予特殊对待。在GCC处理系统标头时,除“ #warning”(请参阅​​“诊断”)生成的警告以外的所有警告均被抑制。

© www.soinside.com 2019 - 2024. All rights reserved.