简体   繁体   中英

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.

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)

Why not have the #include statements inside the logic? Something like

#ifdef _WINDOWS
#include "TestPar.h"
#else
#include "Par.h" 
#endif

and move the #define into their respective include files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM