Consider the following - I want to check with #if #endif whether a
token is defined somewhere in the code.
I am using a CONCAT(input) macro that should glue the constant and changing parts of the token that I want to check.
Unfortunately, the approach presented below causes a compilation error:
error: missing binary operator before token "("
I have found the expressions that can be put inside a #if #endif block:
https://gcc.gnu.org/onlinedocs/cpp/If.html#If
And apparently it states that:
Macros. All macros in the expression are expanded before actual computation of the expression’s value begins.
It turns out that (CONCAT(test)) should be resolved, but it is not.
Is there any workaround allowing to resolve concatenated token names correctly in a conditional compilation block?
#include <stdio.h>
#define CONCAT(input) string##input
#define stringtest 1
int main(void)
{
#if defined(CONCAT(test)) && (CONCAT(test)==1)
printf("OK");
#else
printf("NOT");
#endif
return 0;
}
If you use just:
#if CONCAT(test) == 1it will work and is enought. The statement#if defined(CONCAT(test))does not work becauseCONCAT(test)will be evaluated tostringtestwhich will be evaluated to1, and you can't usedefinedon a numerical constant.You could handle
== 0equalto not defined. So you could use:#if CONCAT(test) != 0 && CONCAT(test) != 1whereCONCAT(test) != 0meansdefined(CONCAT(test)). That is the only alternative, because you can't get macro expansion work in a#ifdefor#if defined()statement, see this question which is very similar to yours.The gcc documentation says:
Also it can be helpful if you check macro expansion by using
gcc -E yourSource.cpp.gcc --help: