#ifndef's before including the headers (redundant inclusion test?)

Matthew Dillon dillon at apollo.backplane.com
Sun Jun 3 13:49:48 PDT 2007

:Why is the following test necessary before including headers? (for
:example in sys/netgraph/netgraph.h).
:#ifndef _SYS_QUEUE_H_
:#include <sys/queue.h>
:when <sys/queue.h> already tests for inclusion?
:#ifndef _SYS_QUEUE_H_
:#define _SYS_QUEUE_H_

    It isn't necessarily, it just prevents GCC from re-scanning header
    files that it has already processed.  It just speeds up compilation
    a bit.

    GCC does have an option to do that sort of thing automatically but
    I really disagree with 'features' like that which break the C
    language and allow really sloppy programming.

    In anycase, for compilation performance one doesn't have to do it
    with every #include (for example, I almost never bother in .C files),
    but doing it in some of the more recursive header files greatly
    reduces the load on the preprocessor.

					Matthew Dillon 
					<dillon at backplane.com>

More information about the Kernel mailing list