cmacrosclangc-preprocessor

Is there a way to use a preprocessor macro inside of another function-like macro?


I found a neat Clang-specific feature that lets you know if a header exists before actually including it (__has_include). I was trying to come up with my own macro to do something like the following:

#define DOES_HEADER_EXIST(_header_)      \
#if !__has_include(_header_)             \
#warning Header not found: _header_      \
#endif

but this does not seem to work. I can see why this would not work, as it's kind of like asking the preprocessor to do two passes, but maybe I'm wrong?


Solution

  • There is no way to create a macro that will expand into a preprocessor directive that the preprocessor will actually take action on.

    Your particular macro is flawed because it expands into a single long line that would have been considered a malformed #if directive, if the compiler would have allowed the # to be followed by a token that wasn't a macro parameter.

    To do what you want, you will need a pre-preprocessor that injects the tests you want to perform before you actually compile the code.