Search code examples
cincludepreprocessor

How to use a #define inside a C include statement?


I have a header file that selects between two platforms:

#pragma once

#ifdef _WINDOWS
#define PAR_CLASS           TestPar
#define PAR_INCLUDE_FILE    "TestPar.h"
#else
#define PAR_CLASS           Par
#define PAR_INCLUDE_FILE    "Par.h" 
#endif

With this I can use the following line to include a header file:

#include "ClassNames.h" 

#include PAR_INCLUDE_FILE

However, I expect more classes and for the PAR_CLASS and PAR_INCLUDE_FILE, the only different is the " and the .h What I would like is to use the PAR_CLASS inside the #include, something like:

#include "PAR_CLASS.h"

But this does not work... Is something like this possible?

I want it to work in both Visual Studio (2019) and Arduino IDE.


Solution

  • You can implement it using the following macro structure:

    #define stringify(x) #x
    #define GEN_INC_PATH(a) stringify(a.h)
    
    #include GEN_INC_PATH(PAR_CLASS)