cpp: Computed Includes
1
1 2.6 Computed Includes
1 =====================
1
1 Sometimes it is necessary to select one of several different header
1 files to be included into your program. They might specify
1 configuration parameters to be used on different sorts of operating
1 systems, for instance. You could do this with a series of conditionals,
1
1 #if SYSTEM_1
1 # include "system_1.h"
1 #elif SYSTEM_2
1 # include "system_2.h"
1 #elif SYSTEM_3
1 ...
1 #endif
1
1 That rapidly becomes tedious. Instead, the preprocessor offers the
1 ability to use a macro for the header name. This is called a "computed
1 include". Instead of writing a header name as the direct argument of
1 '#include', you simply put a macro name there instead:
1
1 #define SYSTEM_H "system_1.h"
1 ...
1 #include SYSTEM_H
1
1 'SYSTEM_H' will be expanded, and the preprocessor will look for
1 'system_1.h' as if the '#include' had been written that way originally.
1 'SYSTEM_H' could be defined by your Makefile with a '-D' option.
1
1 You must be careful when you define the macro. '#define' saves
1 tokens, not text. The preprocessor has no way of knowing that the macro
1 will be used as the argument of '#include', so it generates ordinary
1 tokens, not a header name. This is unlikely to cause problems if you
1 use double-quote includes, which are close enough to string constants.
1 If you use angle brackets, however, you may have trouble.
1
1 The syntax of a computed include is actually a bit more general than
1 the above. If the first non-whitespace character after '#include' is
1 not '"' or '<', then the entire line is macro-expanded like running text
1 would be.
1
1 If the line expands to a single string constant, the contents of that
1 string constant are the file to be included. CPP does not re-examine
1 the string for embedded quotes, but neither does it process backslash
1 escapes in the string. Therefore
1
1 #define HEADER "a\"b"
1 #include HEADER
1
1 looks for a file named 'a\"b'. CPP searches for the file according to
1 the rules for double-quoted includes.
1
1 If the line expands to a token stream beginning with a '<' token and
1 including a '>' token, then the tokens between the '<' and the first '>'
1 are combined to form the filename to be included. Any whitespace
1 between tokens is reduced to a single space; then any space after the
1 initial '<' is retained, but a trailing space before the closing '>' is
1 ignored. CPP searches for the file according to the rules for
1 angle-bracket includes.
1
1 In either case, if there are any tokens on the line after the file
1 name, an error occurs and the directive is not processed. It is also an
1 error if the result of expansion does not match either of the two
1 expected forms.
1
1 These rules are implementation-defined behavior according to the C
1 standard. To minimize the risk of different compilers interpreting your
1 computed includes differently, we recommend you use only a single
1 object-like macro which expands to a string constant. This will also
1 minimize confusion for people reading your program.
1