In either case if there are any tokens on the line

Info icon This preview shows pages 16–18. Sign up to view the full content.

View Full Document Right Arrow Icon
searches for the file according to the rules for angle-bracket includes. In either case, if there are any tokens on the line after the file name, an error occurs and the directive is not processed. It is also an error if the result of expansion does not match either of the two expected forms. These rules are implementation-defined behavior according to the C standard. To min- imize the risk of different compilers interpreting your computed includes differently, we recommend you use only a single object-like macro which expands to a string constant. This will also minimize confusion for people reading your program. 2.7 Wrapper Headers Sometimes it is necessary to adjust the contents of a system-provided header file without editing it directly. GCC’s fixincludes operation does this, for example. One way to do that would be to create a new header file with the same name and insert it in the search path before the original header. That works fine as long as you’re willing to replace the old header entirely. But what if you want to refer to the old header from the new one? You cannot simply include the old header with ‘ #include ’. That will start from the beginning, and find your new header again. If your header is not protected from multiple inclusion (see Section 2.4 [Once-Only Headers], page 10 ), it will recurse infinitely and cause a fatal error. You could include the old header with an absolute pathname: #include "/usr/include/old-header.h" This works, but is not clean; should the system headers ever move, you would have to edit the new headers to match. There is no way to solve this problem within the C standard, but you can use the GNU extension ‘ #include_next ’. It means, “Include the next file with this name”. This directive works like ‘ #include ’ except in searching for the specified file: it starts searching the list of header file directories after the directory in which the current file was found. Suppose you specify ‘ -I /usr/local/include ’, and the list of directories to search also includes ‘ /usr/include ’; and suppose both directories contain ‘ signal.h ’. Ordinary #include <signal.h> finds the file under ‘ /usr/local/include ’. If that file contains #include_next <signal.h> , it starts searching after that directory, and finds the file in /usr/include ’. #include_next ’ does not distinguish between < file > and " file " inclusion, nor does it check that the file you specify has the same name as the current file. It simply looks for the file named, starting with the directory in the search path after the one where the current file was found.
Image of page 16

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 3: Macros 13 The use of ‘ #include_next ’ can lead to great confusion. We recommend it be used only when there is no other alternative. In particular, it should not be used in the headers belonging to a specific program; it should be used only to make global corrections along the lines of fixincludes .
Image of page 17
Image of page 18
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern