graywolf
graywolf

Reputation: 7470

C++ `ifdef` with concatenation of macros values

Can I achieve something similar to following code:

#define MODULE base
#if defined (MODULE ## _dll)      <-- this should do `#ifdef base_dll`
    ...
#else
    ...
#endif

second line is obviously wrong. Can I do this somehow?

Thanks

Upvotes: 8

Views: 1016

Answers (1)

EmDroid
EmDroid

Reputation: 6066

I don't think it is possible to check the definition of token-pasted macro like that (at least I don't know the way) but you can do this:

#define JOIN_INTERNAL(a,b) a ## b
#define JOIN(a,b) JOIN_INTERNAL(a,b)

// switch 1/0
#define base_dll 1

#define MODULE base

#if JOIN(MODULE,_dll)
// the base_dll is 1
#else
// the base_dll is 0 or not defined (in MSVC at least)
#endif

Perhaps if you describe what do you actually want to achieve there might be another way to do that.

Upvotes: 7

Related Questions