{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Iquote should be used instead i i is not the same as

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

View Full Document Right Arrow Icon
-iquote ’ should be used instead. -I. -I- ’ is not the same as no ‘ -I ’ options at all, and does not cause the same behavior for ‘ <> ’ includes that ‘ "" ’ includes get with no special options. ‘ -I. ’ searches the compiler’s current working directory for header files. That may or may not be the same as the directory containing the current file. If you need to look for headers in a directory named ‘ - ’, write ‘ -I./- ’. There are several more ways to adjust the header search path. They are generally less useful. See Chapter 12 [Invocation], page 56 . 2.4 Once-Only Headers If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time. The standard way to prevent this is to enclose the entire real contents of the file in a conditional, like this: /* File foo. */ #ifndef FILE_FOO_SEEN #define FILE_FOO_SEEN the entire file #endif /* !FILE_FOO_SEEN */ This construct is commonly known as a wrapper #ifndef . When the header is included again, the conditional will be false, because FILE_FOO_SEEN is defined. The preprocessor will skip over the entire contents of the file, and the compiler will not see it twice. CPP optimizes even further. It remembers when a header file has a wrapper ‘ #ifndef ’. If a subsequent ‘ #include ’ specifies that header, and the macro in the ‘ #ifndef ’ is still defined, it does not bother to rescan the file at all. You can put comments outside the wrapper. They will not interfere with this optimiza- tion. The macro FILE_FOO_SEEN is called the controlling macro or guard macro . In a user header file, the macro name should not begin with ‘ _ ’. In a system header file, it should begin with ‘ __ ’ to avoid conflicts with user programs. In any kind of header file, the macro name should contain the name of the file and some additional text, to avoid conflicts with other header files. 2.5 Alternatives to Wrapper #ifndef CPP supports two more ways of indicating that a header file should be read only once. Neither one is as portable as a wrapper ‘ #ifndef ’ and we recommend you do not use them in new programs, with the caveat that ‘ #import ’ is standard practice in Objective-C. CPP supports a variant of ‘ #include ’ called ‘ #import ’ which includes a file, but does so at most once. If you use ‘ #import ’ instead of ‘ #include ’, then you don’t need the
Image of page 14

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

View Full Document Right Arrow Icon
Chapter 2: Header Files 11 conditionals inside the header file to prevent multiple inclusion of the contents. ‘ #import is standard in Objective-C, but is considered a deprecated extension in C and C ++ . #import ’ is not a well designed feature. It requires the users of a header file to know that it should only be included once. It is much better for the header file’s implementor to write the file so that users don’t need to know this. Using a wrapper ‘ #ifndef ’ accomplishes this goal.
Image of page 15
Image of page 16
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