Next: , Up: Header Files   [Contents][Index]


5.6.1 Portability of Headers

This section documents some collected knowledge about common headers, and the problems they cause. By definition, this list always requires additions. A much more complete list is maintained by the Gnulib project (see Gnulib), covering Posix Headers in Gnulib and Glibc Headers in Gnulib. Please help us keep the Gnulib list as complete as possible.

When we say that a header “may require” some set of other headers, we mean that it may be necessary for you to manually include those other headers first, or the contents of the header under test will fail to compile. When checking for these headers, you must provide the potentially-required headers in the includes argument to AC_CHECK_HEADER or AC_CHECK_HEADERS, or the check will fail spuriously. AC_INCLUDES_DEFAULT (see Default Includes) arranges to include a number of common requirements and should normally come first in your includes. For example, net/if.h may require sys/types.h, sys/socket.h, or both, and AC_INCLUDES_DEFAULT handles sys/types.h but not sys/socket.h, so you should check for it like this:

AC_CHECK_HEADERS([sys/socket.h])
AC_CHECK_HEADERS([net/if.h], [], [],
[AC_INCLUDES_DEFAULT[
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
]])

Note that the example mixes single quoting (forAC_INCLUDES_DEFAULT, so that it gets expanded) and double quoting (to ensure that each preprocessor # gets treated as a literal string rather than a comment).

limits.h

In C99 and later, limits.h defines LLONG_MIN, LLONG_MAX, and ULLONG_MAX, but many almost-C99 environments (e.g., default GCC 4.0.2 + glibc 2.4) do not define them.

memory.h

This header file is obsolete; use string.h instead.

strings.h

On some systems, this is the only header that declares strcasecmp, strncasecmp, and ffs.

This header may or may not include string.h for you. However, on all recent systems it is safe to include both string.h and strings.h, in either order, in the same source file.

inttypes.h vs. stdint.h

C99 specifies that inttypes.h includes stdint.h, so there’s no need to include stdint.h separately in a standard environment. However, some implementations have inttypes.h but not stdint.h (e.g., Solaris 7), and some have stdint.h but not inttypes.h (e.g. MSVC 2012). Therefore, it is necessary to check for each and include each only if available.

linux/irda.h

This header may require linux/types.h and/or sys/socket.h.

linux/random.h

This header may require linux/types.h.

net/if.h

This header may require sys/types.h and/or sys/socket.h.

netinet/if_ether.h

This header may require some combination of sys/types.h, sys/socket.h, netinet/in.h, and net/if.h.

sys/mount.h

This header may require sys/params.h.

sys/ptem.h

This header may require sys/stream.h.

sys/socket.h

This header may require sys/types.h.

sys/ucred.h

This header may require sys/types.h.

X11/extensions/scrnsaver.h

Using XFree86, this header requires X11/Xlib.h, which is probably so required that you might not even consider looking for it.


Next: Particular Header Checks, Up: Header Files   [Contents][Index]