2

Is there any practical use for macro expansion on an "#include" line?

For example, a header file like

// calculated_include.h
#define THE_STRING "Hello, World!\n"

Then a C or C++ file like

// calculated_include.c
#define HEADER_MACRO "calculated_include.h"
#define STDIO <stdio.h>

#include STDIO
#include HEADER_MACRO

int main(void){
        printf("%s", THE_STRING );
}

Then from the command line I do

make calculated_include
./calculated_include

and it prints out, as you've probably already guessed,

Hello, World!

I see some people call macro expansion on an "#include" line "calculated includes" or "computed includes"; is there a better name?

I see that one person wanted to use text substitution in a "#include" file so a filename specified on the gcc command line with the "-D" option would get substituted into an "#include" statement ( How can I specify an include file from the GCC Command Line? ). But it seems to me that specifying that filename directly with the "-include" command-line option instead ( Include header files using command line option? and "Options Controlling the Preprocessor") would be simpler and work just as well.

Is there any other use for macro expansion on an "#include" line?

Community
  • 1
  • 1
David Cary
  • 5,250
  • 6
  • 53
  • 66
  • You could use it to include header files for another architecture (different CPU or platform) or for specifying a build variant (e.g. Debug or Release). Your macro would then define the path to look for the include files. Obviously, there are other ways of achieving this. – NZD Aug 15 '15 at 02:02
  • I think you mean the `-include` option, but anyway that is gcc-specific, and other compilers might not have an analogous option. On the other hand, pretty well every compiler has an equivalent to `-D`. – rici Aug 16 '15 at 15:50
  • @rici: Thank you, the "-include" option is exactly what I meant. – David Cary Aug 16 '15 at 17:44

1 Answers1

0

one use case is that, it helps you to select one of several different header files to be included into your program. check this link

n devaraj
  • 134
  • 9
  • I feel that you didn't actually read the question, because I already mentioned that "use case" and that link in the last 4 sentences of the question. – David Cary Jan 03 '16 at 18:50