Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 938
graywolf Avatar asked Dec 05 '15 18:12

graywolf


People also ask

What is meant by concatenation of macro parameters?

Concatenation means joining two strings into one. In the context of macro expansion, concatenation refers to joining two lexical units into one longer one. Specifically, an actual argument to the macro can be concatenated with another actual argument or with fixed text to produce a longer name.

What is concatenation operator in embedded C?

This is called token pasting or token concatenation. The '##' pre-processing operator performs token pasting. When a macro is expanded, the two tokens on either side of each '##' operator are combined into a single token, which then replaces the '##' and the two original tokens in the macro expansion.

What is __va_args __?

Here also we have to include ellipsis, The '__VA_ARGS__' is used to handle variable length arguments. Concatenation operator '##' is used to concatenate the variable arguments. In this example, the Macro will take variable length argument like the printf() or scanf() function.

What does ## mean in macro?

The double-number-sign or token-pasting operator (##), which is sometimes called the merging or combining operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token, and therefore, can't be the first or last token in the macro definition.


1 Answers

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.

like image 88
EmDroid Avatar answered Sep 24 '22 18:09

EmDroid