禁用系统 headers 的某些警告
Disable certain warnings for system headers
我通常使用 -Werror
编译我的项目并打开一些警告(例如 -Wsequence-point -Wcast-align -Wstrict-prototypes -Wstrict-aliasing
)。
使用这些设置,在某些平台上,一些 headers 在包含时会产生警告(由于第一个开关而变成错误)。例如,我在 MacOS 上看到一些 X11 headers。
我不想降低我的代码的质量标准。有没有办法在不全局禁用有问题的警告的情况下编译我的项目?例如,有没有一种方法可以仅针对包含在我的项目中的 headers 禁用警告?
编辑
这是我要解决的问题的示例:
gcc -std=c99 -pthread -O2 -fstrict-aliasing -I/usr/X11/include -Werror -Wpedantic -Wstrict-aliasing -Wchar-subscripts -Wimplicit -Wsequence-point -Wwrite-strings -Wunused-variable -Wvla -c -o main.o main.c
/usr/X11/include/X11/Xfuncproto.h:145:24: error: named variadic macros are a GNU extension [-Werror,-Wvariadic-macros]
#define _X_NONNULL(args...) __attribute__((nonnull(args)))
Options for Directory Search 上的 GCC 手册列出:
These options specify directories to search for header files, for libraries and for parts of the compiler:
-I dir
-iquote dir
-isystem dir
-idirafter dir
Add the directory dir
to the list of directories to be searched for header files during preprocessing. If dir
begins with ‘=
’, then the ‘=
’ is replaced by the sysroot
prefix; see --sysroot
and -isysroot
.
Directories specified with -iquote
apply only to the quote form of the directive, #include "file"
. Directories specified with -I
, -isystem
, or -idirafter
apply to lookup for both the #include "file"
and #include <file>
directives.
You can specify any number or combination of these options on the command line to search for header files in several directories. The lookup order is as follows:
- For the quote form of the include directive, the directory of the current file is searched first.
- For the quote form of the include directive, the directories specified by
-iquote
options are searched in left-to-right order, as they appear on the command line.
- Directories specified with
-I
options are scanned in left-to-right order.
- Directories specified with
-isystem
options are scanned in left-to-right order.
- Standard system directories are scanned.
- Directories specified with
-idirafter
options are scanned in left-to-right order.
You can use -I
to override a system header file, substituting your own version, since these directories are searched before the standard system header file directories. However, you should not use this option to add directories that contain vendor-supplied system header files; use -isystem
for that.
The -isystem
and -idirafter
options also mark the directory as a system directory, so that it gets the same special treatment that is applied to the standard system directories.
If a standard system include directory, or a directory specified with -isystem
, is also specified with -I
, the -I
option is ignored. The directory is still searched but as a system directory at its normal position in the system include chain. This is to ensure that GCC’s procedure to fix buggy system headers and the ordering for the #include_next
directive are not inadvertently changed. If you really need to change the search order for system directories, use the -nostdinc
and/or -isystem
options.
倒数第二段引用的注释指出,使用 -isystem
指定目录会抑制警告,因为它们对任何其他系统 header(默认情况下)都是抑制的。
手册的 Options to Request or Suppress Warnings 部分包括:
-Wsystem-headers
Print warning messages for constructs found in system header files. Warnings from system headers are normally suppressed, on the assumption that they usually do not indicate real problems and would only make the compiler output harder to read. Using this command-line option tells GCC to emit warnings from system headers as if they occurred in user code. However, note that using -Wall
in conjunction with this option does not warn about unknown pragmas in system headers—for that, -Wunknown-pragmas
must also be used.
因此,将包含 /usr/X11/include/X11/Xfuncproto.h
文件的目录指定为系统目录:
-isystem /usr/X11/include
您不应再收到来自 #include <X11/Xfuncproto.h>
的警告。
我通常使用 -Werror
编译我的项目并打开一些警告(例如 -Wsequence-point -Wcast-align -Wstrict-prototypes -Wstrict-aliasing
)。
使用这些设置,在某些平台上,一些 headers 在包含时会产生警告(由于第一个开关而变成错误)。例如,我在 MacOS 上看到一些 X11 headers。
我不想降低我的代码的质量标准。有没有办法在不全局禁用有问题的警告的情况下编译我的项目?例如,有没有一种方法可以仅针对包含在我的项目中的 headers 禁用警告?
编辑
这是我要解决的问题的示例:
gcc -std=c99 -pthread -O2 -fstrict-aliasing -I/usr/X11/include -Werror -Wpedantic -Wstrict-aliasing -Wchar-subscripts -Wimplicit -Wsequence-point -Wwrite-strings -Wunused-variable -Wvla -c -o main.o main.c
/usr/X11/include/X11/Xfuncproto.h:145:24: error: named variadic macros are a GNU extension [-Werror,-Wvariadic-macros]
#define _X_NONNULL(args...) __attribute__((nonnull(args)))
Options for Directory Search 上的 GCC 手册列出:
These options specify directories to search for header files, for libraries and for parts of the compiler:
-I dir
-iquote dir
-isystem dir
-idirafter dir
Add the directory
dir
to the list of directories to be searched for header files during preprocessing. Ifdir
begins with ‘=
’, then the ‘=
’ is replaced by thesysroot
prefix; see--sysroot
and-isysroot
.Directories specified with
-iquote
apply only to the quote form of the directive,#include "file"
. Directories specified with-I
,-isystem
, or-idirafter
apply to lookup for both the#include "file"
and#include <file>
directives.You can specify any number or combination of these options on the command line to search for header files in several directories. The lookup order is as follows:
- For the quote form of the include directive, the directory of the current file is searched first.
- For the quote form of the include directive, the directories specified by
-iquote
options are searched in left-to-right order, as they appear on the command line.- Directories specified with
-I
options are scanned in left-to-right order.- Directories specified with
-isystem
options are scanned in left-to-right order.- Standard system directories are scanned.
- Directories specified with
-idirafter
options are scanned in left-to-right order.You can use
-I
to override a system header file, substituting your own version, since these directories are searched before the standard system header file directories. However, you should not use this option to add directories that contain vendor-supplied system header files; use-isystem
for that.The
-isystem
and-idirafter
options also mark the directory as a system directory, so that it gets the same special treatment that is applied to the standard system directories.If a standard system include directory, or a directory specified with
-isystem
, is also specified with-I
, the-I
option is ignored. The directory is still searched but as a system directory at its normal position in the system include chain. This is to ensure that GCC’s procedure to fix buggy system headers and the ordering for the#include_next
directive are not inadvertently changed. If you really need to change the search order for system directories, use the-nostdinc
and/or-isystem
options.
倒数第二段引用的注释指出,使用 -isystem
指定目录会抑制警告,因为它们对任何其他系统 header(默认情况下)都是抑制的。
手册的 Options to Request or Suppress Warnings 部分包括:
-Wsystem-headers
Print warning messages for constructs found in system header files. Warnings from system headers are normally suppressed, on the assumption that they usually do not indicate real problems and would only make the compiler output harder to read. Using this command-line option tells GCC to emit warnings from system headers as if they occurred in user code. However, note that using
-Wall
in conjunction with this option does not warn about unknown pragmas in system headers—for that,-Wunknown-pragmas
must also be used.
因此,将包含 /usr/X11/include/X11/Xfuncproto.h
文件的目录指定为系统目录:
-isystem /usr/X11/include
您不应再收到来自 #include <X11/Xfuncproto.h>
的警告。