There are two types of header files in Mac OS X:
Header files serve four functions:
Unix developers will find the ordinary header files familiar, since they follow the BSD convention. The C preprocessor directive #include includes a header file in a C source file. There are essentially three forms of this syntax:
You can use #include, followed by a macro, which, when expanded, must be in one of the aforementioned forms. As noted in the previous chapter, frameworks in Mac OS X are common when you step outside of the BSD portions of the operating system. To include a framework header file in Objective-C code, use the following format: #import < frameworkname/headerfilename .h> where frameworkname is the name of the framework without the extension and headerfilename is the name of the header file. For example, the included declaration for a Cocoa application would look like: #import <Cocoa/Cocoa.h> Note that you must use #include rather than #import when including a framework in Carbon code. When preprocessing header files or any preprocessor directives, the following three actions are always taken:
Keep the following rules in mind:
9.1.1 Precompiled Header FilesMac OS X's Xcode Tools support and provide extensive documentation on building and using precompiled header files. This section highlights a few of the issues that may be of interest to Unix developers new to Mac OS X when it comes to working with precompiled headers. Precompiled header files are binary files that have been generated from ordinary C header files, and then preprocessed and parsed using cpp-precomp . When such a precompiled header is created, both macros and declarations present in the corresponding ordinary header file are sorted, resulting in a faster compile time, a reduced symbol table size, and consequently, faster lookup. Precompiled header files are given a .p extension and are produced from ordinary header files that end with a .h extension. There is no risk that a precompiled header file will get out of sync with the .h file, because the compiler checks the timestamp of the actual header file. When using precompiled header files, you should not refer to the .p version of the name, but rather to the .h version in the #include directive. If a precompiled version of the header file is available, it is used automatically; otherwise , the real header file ( .h ) is used. So, to include foo.p , specify foo.h . The fact that cc is using a precompiled header is totally hidden from you. In addition to checking the timestamp, the preprocessor also checks whether the current context is the same as the context in which the precompilation was performed. For the precompiled header to be used, the timestamp needs to indicate that the modification time of the .p version is more recent than the .h version, and therefore, that the contexts are equivalent. The context is the amalgamation of all defines (#define) in place at the time you compile a program. If the defines are different the next time you include the .h file, cpp-precomp will regenerate the .p file based on the current set of defines. Mac OS X system headers are precompiled. For example, AppKit.p , Cocoa.p , mach.p , and other precompiled header files are stored in /System/Library/Frameworks . You can create your own precompiled header files using the cc - precomp compile driver flag. For example, the following command illustrates this process in its simplest, context-independent form: cc -precomp header .h -o header .p If there is context dependence (for example, some conditional compilation), the - Dsymbol flag is used. In this case, the command to build a precompiled header file (with the FOO symbol defined) is: cc -precomp -DFOO header .h -o header .p For more details on building and using precompiled header files, as well as using the cpp-precomp preprocessor, read the documentation stored in the /Developer/Documentation/DeveloperTools/ Preprocessor/ directory.
A complete list of precompiled headers can be found in the phase1.precompList and phase2.precompList files, located in /System/Library/SystemResources/PrecompLists . Table 9-1 lists the contents of the files. Table 9-1. Precompiled header files, as listed in phase1.precompList andphase2.precompList
Although the filenames in phase1.precompList and phase2.precompList are listed as filename.p (for example, libc.p ), the actual file used depends on the compiler version. For example, gcc3 will use libc-gcc3.p .
9.1.1.1 PFE precompilationThe gcc3.3 compiler supports an alternative precompilation mechanism called Persistent Front End (PFE). This mechanism offers the same performance benefits as cpp-precomp , but supports C++ and Objective-C++. ( cpp-precomp does not support either language.) To precompile a header file with PFE, compile the header, specifying the ”dump-pch switch with the name of the output file. You'll also need to supply the language with the - x switch (see Section 8.2.3 in Chapter 8): gcc -x c --dump-pch header .pfe header .h Then, you can compile main.c by using the ”load-pch switch and supplying the name of the precompiled file: gcc --load-pch header.pfe main.c -o main Example 9-1 shows header.h . Example 9-1. The header.h file/* header.h: a trivial header file. */ #define x 100 Example 9-2. The main.c application/* main.c: a simple program that includes header.h. */ #include <stdio.h> #include "header.h" int main( ) { printf("%d\n", x); return 0; } 9.1.2 malloc.hmake may fail to compile some types of Unix software if it cannot find malloc.h . Software designed for older Unix systems may expect to find this header file in /usr/include ; however, malloc.h is not present in this directory. The set of malloc( ) function prototypes is actually found in stdlib.h . For portability, your programs should include stdlib.h instead of malloc.h . (This is the norm; systems that require malloc.h are the rare exception these days.) GNU autoconf will detect systems that require malloc.h and define the HAVE_MALLOC_H macro. If you do not use GNU autoconf , you will need to detect this case on your own and set the macro accordingly . You can handle such cases with this code: #include <stdlib.h> #ifdef HAVE_MALLOC_H #include <malloc.h> #endif For a list of libraries that come with Mac OS X, see Section 9.10, later in this chapter. 9.1.3 poll.hOne issue in porting software from a System V platform to a BSD platform (e.g., Mac OS X) is the lack of the poll( ) system call function, which provides a mechanism for I/O multiplexing. Panther provides this function through emulation, which makes use of its BSD analog select( ) . The associated header file, /usr/include/poll.h , is included with Panther. 9.1.4 wchar .h and iconv.hAnother issue in porting Unix software to previous versions of Mac OS X was the relatively weak support for wide (i.e., more than 8-bits) character datatypes (e.g., Unicode). Panther improves this situation by including the GNU libiconv , which provides the iconv( ) function to convert between various text encodings. Additionally, the wchar_t type is supported in Panther. The header files iconv.h and wchar.h are also included. Alternatively, you can use the APIs available in the CoreFoundation's String services, which are described in CFString.h . 9.1.5 dlfcn.hThis header file, along with its associated dlcompat library functions, is included in Panther. The dlcompat library functions such as dlopen( ) are actually included in libSystem . 9.1.6 alloc .hAlthough this header file is not included with Mac OS X, its functionality is provided by stdlib.h . If your code makes a specific request to include alloc.h , you have several choices. One option is to remove the #include <alloc.h> statement in your source code. This may be cumbersome, however, if your include statement appears in many files. Another alternative is to create your own version of alloc.h . A sample alloc.h is suggested in The Apple Developer Connection's Technical Note TN2071 (http://developer.apple.com/technotes/tn2002/tn2071.html). 9.1.7 lcyrpt.hAlthough lcrypt.h is not included in Mac OS X, its functionality is provided in unistd.h . 9.1.8 values.hThe values.h file, another header file found on many Unix systems, is not included in Mac OS X. Its functionality, however, is provided by limits.h . |