Search code examples
cgccincludegcc-warning

Disable certain warnings for system headers


I'm usually compiling my projects with -Werror and some warnings turned on (like -Wsequence-point -Wcast-align -Wstrict-prototypes -Wstrict-aliasing).

With these settings, on some platforms some headers produce warnings when included (which turn into errors because of the first switch). For example I've seen this with some X11 headers on MacOS.

I don't want to degrade the quality standards for my code. Is there a way to make my project compile without disabling problematic warnings globally? For example is there a way to disable warnings only for included headers out of my project?

EDIT

Here is an example of the problem I'm trying to solve:

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

Solution

  • The GCC manual on Options for Directory Search lists:

    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.

    The second last paragraph quoted notes that designating directories with -isystem suppresses the warnings as they are suppressed for any other system header (by default).

    The Options to Request or Suppress Warnings section of the manual includes:

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

    So, by designating the directory that contains the /usr/X11/include/X11/Xfuncproto.h file as a system directory with:

    -isystem /usr/X11/include
    

    you should not get warnings from #include <X11/Xfuncproto.h> any more.