I need to display some relevant information in #error preprocessor directive. For example:
#define myConstant 5
#if myConstant > 10
#error myConstant has to be > 10, now %d //should display "5"
#endif
How can I do that?
You can't. #error
doesn't allow/support that.
Instead, use _Static_assert
, from C11:
#define myConstant 5
#define STRINGIFY(x) STRINGIFY_(x)
#define STRINGIFY_(x) #x
_Static_assert(myConstant > 10, "myConstant has to be > 10, now " STRINGIFY(myConstant));
Output:
test.c:5:1: error: static assertion failed: "myConstant has to be > 10, now 5" _Static_assert(myConstant > 10, "myConstant has to be > 10, now " STRINGIFY(myConstant)); ^~~~~~~~~~~~~~
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With