clang++ 是否以更轻松的方式处理系统标头?
Does clang++ treat system headers in a more relaxed way?
以下结构在 Linux 系统头文件/usr/include/sys/inotify.h
中定义:
struct inotify_event
{
int wd;
uint32_t mask;
uint32_t cookie;
uint32_t len;
char name __flexarr;
};
请注意最后一个字段name
,它是一个零长度数组。C++17 不支持零长度数组,因此,如果在 C++17 项目中使用struct inotify_event
并使用-pedantic
编译它,则应引发编译器警告。
但是,以下代码不会引发任何警告struct inotify_event
。更奇怪的是,如果我以相同的方式使用零长度数组定义结构,则会按预期引发警告。
编译器选项:clang++ -std=c++17 -pedantic main.cpp
#include <sys/inotify.h>
struct inotify_event* p = nullptr; // no warning
struct A
{
int x;
char name __flexarr; // warning: flexible array members are a C99 feature
};
int main()
{}
clang++ 背后是否有任何魔力可以更轻松地处理系统标头?
clang++ 背后有什么魔力可以更轻松地处理系统定义的标头吗?
是的,如果我们查看"控制系统标头中的诊断"的 clang 文档,它说:
当警告出现在系统标头中时,将禁止显示警告。默认情况下,如果在
-isystem
指定的包含路径中找到包含的文件,则该文件将被视为系统标头,但这可以通过多种方式覆盖。
system_header
杂注可用于将当前文件标记为系统标头。从杂注的位置开始,不会在同一文件中生成警告。#if foo #endif foo // warning: extra tokens at end of #endif directive #pragma clang system_header #if foo #endif foo // no warning
–system-header-prefix=
和–no-system-header-prefix=
命令行参数可用于覆盖是否将包含路径的子集视为系统标头。当#include
指令中的名称在标头搜索路径中找到并以系统前缀开头时,该标头将被视为系统标头。命令行上与指定标头名称匹配的最后一个前缀优先。例如:
$ clang -Ifoo -isystem bar --system-header-prefix=x/ --no-system-header-prefix=x/y/
在这里,
#include "x/a.h"
被视为包含系统标头,即使在foo
中找到标头,#include "x/y/b.h"
也被视为不包含系统标头,即使在bar
中找到标头也是如此。如果将包含文件视为系统头,则查找相对于当前目录的文件的
#include
指令将被视为包含系统头。
- 错误处理.将系统错误代码映射到泛型
- 我可以使用哪种数据结构来处理这种方式
- 如何编写具有相同名称的相同函数,该函数在C++中几乎以相似的方式处理不同的类参数?
- C++事件系统 - 多态事件和事件处理程序
- clang++ 是否以更轻松的方式处理系统标头?
- 如何以这种方式处理字符串数组
- Boost进程"系统"功能中的错误处理
- 处理无法按预期方式创建的对象,c++
- 即使在多任务处理时,添加用户时间 + 系统时间(来自 shell 的时间命令)是否是一种可靠的措施?
- 以跨平台的方式C++处理键盘
- 跨平台套接字发送,Linux 上的缓冲区常量无效* Windows上的常量字符*,最佳处理方式
- 编译器以不同的方式处理数学?
- 学习如何制作可以处理动画的程序的最佳方式
- 静态链接如何处理系统文件?
- 与其他字符相比,c++/Arduino 处理“?”的方式是否不同
- 如何处理系统链路控制的NM_CLICK
- 处理系统键关闭消息时禁用烦人的声音
- 做系统();命令的工作方式与批处理命令完全相同
- Win32:更多"object oriented"窗口消息处理系统
- 为请求处理系统提供灵活、无阻碍的参数传递