Wed, 18 Oct 2006

A Small Trick: Avoiding missing configuration includes

The kernel used to have a problem that people would use the CONFIG_ pre-processor variables but not include the "linux/config.h" header, resulting in strange bugs as some files were built with the option, and some without.

 #ifdef CONFIG_MODULES
 some expression
 #endif

This was solved in the kernel by putting "-include linux/config.h" on the gcc command line to always ensure its inclusion. But an alternative is to change to using #if instead:

 #if CONFIG_MODULES
 some expression
 #endif

This does the same thing (undefined symbols in CPP are equivalent to 0), but now you can define every switch to 0 or 1 and use -Wundef: GCC will warn if the option is undefined.


[/tech] permanent link