]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/doc/cpp/header-files/wrapper-headers.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / doc / cpp / header-files / wrapper-headers.rst
1 ..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6 .. index:: wrapper headers, overriding a header file, #include_next
7
8 .. _wrapper-headers:
9
10 Wrapper Headers
11 ***************
12
13 Sometimes it is necessary to adjust the contents of a system-provided
14 header file without editing it directly. GCC's :command:`fixincludes`
15 operation does this, for example. One way to do that would be to create
16 a new header file with the same name and insert it in the search path
17 before the original header. That works fine as long as you're willing
18 to replace the old header entirely. But what if you want to refer to
19 the old header from the new one?
20
21 You cannot simply include the old header with :samp:`#include`. That
22 will start from the beginning, and find your new header again. If your
23 header is not protected from multiple inclusion (see :ref:`once-only-headers`), it will recurse infinitely and cause a fatal error.
24
25 You could include the old header with an absolute pathname:
26
27 .. code-block:: c++
28
29 #include "/usr/include/old-header.h"
30
31 This works, but is not clean; should the system headers ever move, you
32 would have to edit the new headers to match.
33
34 There is no way to solve this problem within the C standard, but you can
35 use the GNU extension :samp:`#include_next`. It means, 'Include the
36 *next* file with this name'. This directive works like
37 :samp:`#include` except in searching for the specified file: it starts
38 searching the list of header file directories *after* the directory
39 in which the current file was found.
40
41 Suppose you specify :option:`-I /usr/local/include`, and the list of
42 directories to search also includes :samp:`/usr/include`; and suppose
43 both directories contain :samp:`signal.h`. Ordinary ``#include
44 <signal.h>`` finds the file under :samp:`/usr/local/include`. If that
45 file contains ``#include_next <signal.h>``, it starts searching
46 after that directory, and finds the file in :samp:`/usr/include`.
47
48 :samp:`#include_next` does not distinguish between ``<file>``
49 and ``"file"`` inclusion, nor does it check that the file you
50 specify has the same name as the current file. It simply looks for the
51 file named, starting with the directory in the search path after the one
52 where the current file was found.
53
54 The use of :samp:`#include_next` can lead to great confusion. We
55 recommend it be used only when there is no other alternative. In
56 particular, it should not be used in the headers belonging to a specific
57 program; it should be used only to make global corrections along the
58 lines of :command:`fixincludes`.