c++macrosinclude

Forming a #include file name from a number


Given a numeric macro as input via the C++ compiler's -D option, can I form a filename for use with a #include statement from it? For example, currently I might have:

#include "data/1024_cadr.h"

...but I would like to form this include string through use of -DSIZE=1024 during invocation of the compiler. I would then use this from within a script which drives the compiler, and runs some tests. I would prefer not to include all of the file names explicitly, as the scripts use the numeric value (here 1024) elsewhere.

I tried the following (and other ideas), with no luck:

#define str(x) #x
#include "data/"##str(SIZE)##"_cadr.h"

Solution

  • As answered by @Eljay by comment, the following will do the job:

    #define  MAKE_CADR3(x) #x
    #define  MAKE_CADR2(x) MAKE_CADR3(data/x##_cadr.h)
    #define  MAKE_CADR(x)  MAKE_CADR2(x)
    #include MAKE_CADR(SZ)