C preprocessor - Prepending path for existing define
I know this is not exactly what you ask for. However, instead of doing obscure preprocessor magic what about putting to a header file something like:
#undef BIN_DIR
#define BIN_DIR bin_dir
extern char *bin_dir;
and to one of code files and BEFORE including the above header:
char *bin_dir = PRE_PATH BIN_DIR;
It is not possible to modify the value of a macro without losing its initial value. You have to remember that defining a macro is not equivalent to assigning to a variable. In the latter case, the right-hand expression is evaluated, and the resulting value is assigned. In the former case, you define a name (a macro) for a sequence of tokens, which do not get evaluated until the macro is expanded. So when you define this:
#define TMP BINDIR
the TMP
macro does not "contain" the path "/usr/bin", it contains "BINDIR", literally. When TMP
expands, it expands to BINDIR
, which in turn expands to "/usr/bin". When you undefine BINDIR
, the value it had is lost and TMP
expansion will result in just "BINDIR".
What you could do is to use a different macro for the complete path instead of BINDIR
. Something like this:
#define FULL_BINDIR PRE_PATH BINDIR