Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a macro with defined(X)

Here is a macro I want to use, if X_DEFINED is defined it will be evaluated to DEFAULT_X otherwise it will be evaluated to x

#define GET_X(x) (defined(X_DEFINED) ? DEFAULT_X : x)

It doesn't compile, with the error

error: 'X_DEFINED' was not declared in this scope

Any suggestions? I want to be able to select between a parameter and a global variable based on if X_DEFINED was defined or not

like image 847
e271p314 Avatar asked Mar 22 '26 06:03

e271p314


1 Answers

defined() only works in #if and similar preprocessor directives.

You want something like this:

#ifdef X_DEFINED
#define GET_X(x) DEFAULT_X
#else
#define GET_X(x) x
#endif
like image 66
HolyBlackCat Avatar answered Mar 24 '26 01:03

HolyBlackCat