gpt4 book ai didi

c - 禁用系统 header 的某些警告

转载 作者:行者123 更新时间:2023-12-02 20:35:18 33 4
gpt4 key购买 nike

我通常使用 -Werror 编译项目,并打开一些警告(例如 -Wsequence-point -Wcast-align -Wstrict-prototypes -Wstrict-aliasing )。

通过这些设置,在某些平台上,某些 header 在包含时会产生警告(由于第一次切换而变成错误)。例如,我在 MacOS 上的一些 X11 header 中看到过这种情况。

我不想降低我的代码的质量标准。有没有办法让我的项目编译而不全局禁用有问题的警告?例如,有没有办法仅对我的项目中包含的 header 禁用警告?

编辑

这是我试图解决的问题的示例:

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)))

最佳答案

GCC 手册 Options for Directory Search列表:

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> 的警告不再有。

关于c - 禁用系统 header 的某些警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36355232/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com