15

Consider a (read-only third-party) header lib.h with:

#define XYZ 42

In a source file, I want to use the word XYZ for an unrelated purpose, and do not want the substitution with 42. But, in the same source file, for other purposes, I also do want to access the value 42 from lib.h without hardcoding it. How do I rename the macro from XYZ to, say, LIB_XYZ?

The following does not work, because preprocessor wants XYZ at the time the LIB_XYZ substitution is made, but XYZ had been undefined:

#include "lib.h"
#define LIB_XYZ XYZ
#undef XYZ

Is there a way to trick the preprocessor into expanding LIB_XYZ to its final value before XYZ is lost?

2
  • 2
    I think you are stuck, if you want to get the definition of LIB_XYZ from the third party header file, and don't want to use anything but the C compiler. If I had to do this, I'd probably write a script to transform lib.h into our_lib.h, with selected #define's modified, make my code include our_lib.h, and use a makefile to make the right thing happen if lib.h changed. Commented Jan 13, 2016 at 0:51
  • 1
    Perhaps this can help. Commented Jan 13, 2016 at 0:53

4 Answers 4

8

Not with the pre-processor, at least, not that I am aware of.

However, for simple constants with known type like in your example, there is a workaround.

#include <stdio.h>

// <xyz.h>

#define XYZ 42

// </xyz.h>

enum xyz_constants
{
  LIB_XYZ = XYZ,
};

#undef XYZ

#define XYZ 27

int
main()
{
  printf("old value: %d, new value: %d\n", LIB_XYZ, XYZ);
  return 0;
}

Not showing the fluff from stdio.h, this code is pre-processed to the following.

enum xyz_constants
{
  LIB_XYZ = 42,
};

int
main()
{
  printf("old value: %d, new value: %d\n", LIB_XYZ, 27);
  return 0;
}

You can extend this to some degree to other data types and certain function-like macros but there are of course limits.

Anyway, why do you need the particular identifier XYZ? Can't you use a different name for your macro?

3
  • 1
    The enum trick was brilliant! Thank you! Re why: XYZ is a substring of another third-party macro that I'm constructing via concatenation, and the concatenation macro is generic (works for XYZ or ABC or whatever), so stuck with XYZ.
    – alexei
    Commented Jan 13, 2016 at 1:06
  • This solution just saved me :)
    – MByD
    Commented Feb 21, 2016 at 9:12
  • In the general case, a solution involving #undef XYZ may not work. The library may refer to XYZ in the replacement texts of its macros; those macros will break of XYZ is undefined or redefined to something else. If you know what those macros are and can avoid them, then it's workable.
    – Kaz
    Commented Feb 1, 2019 at 21:01
4

If XYZ from lib.h is a number [or a constant of a variety], you could use an enum:

enum { LIB_XYZ = XYZ };
#undef XYZ

If XYZ is not the above, you have to create (e.g.) myxyz.c that does not include lib.h and use XYZ there (other files may include xyz.h)

The difference is that #define LIB_XYZ XYZ will not be resolved at that line, only when you use it later, as in:

foo(LIB_XYZ);

so that won't work because you've already #undef'ed the XYZ.

0

Use another .c file, and assign the macro value to a global variable.

0

A preprocessor symbol is a name. There is no preprocessing directive which changes the name itself, while preserving the contents. For instance, given either:

#define FOO 42

or

#define FOO(x, y)  x ## y (

There is no way to define a macro called BAR which has the same contents, without repeating these definitions. That is to say, there is no operation like:

#alias BAR FOO  // nonexistent fantasy macro-cloning preprocessor directive

Nor like this:

#rename BAR FOO // like #alias BAR FOO followed by #undef FOO

If we do this:

#define BAR FOO  // for the #define FOO 42 case

it's not an alias. Macro BAR is defined such that its replacement token sequence is the token FOO, and not 42. If the FOO macro disappears, then BAR loses its meaning.

Note also that C preprocessor macros cannot expand into preprocessing directives, so the following approach also will not work:

 // wrong:
 #define MACRO_DEFINER(NAME) \
   #define NAME 42

 MACRO_DEFINER(FOO) // hoping for #define FOO 42: no such luck
 MACRO_DEFINER(BAR) // hoping for #define BAR 42: likewise

I'm afraid that you have to take a few steps back to find an alternative strategy to whatever problem you're trying to solve. If you get stuck, create a new question about the actual problem.

There is always code generation: generating C or C++ at build time. Then any textual substitution or expansion you can dream of is possible, if you just tweak your generation system.

Any solution that involves #undef XYZ risks brekaing the functionality of the library header. The library might do something like this:

#define XYZ 42 #define macro(B) bloop(XYZ, B)

If we are using macro, then we break it if we redefine XYZ.

If the library is known for defining XYZ and we manage to redefine it successfully in code which uses that library, the situation will be confusing to future maintainers. "Oh, this XYZ is not actually that library one; this programmer just wanted an unrelated XYZ."

The best solution here is to stop wanting to use XYZ and find some other name. Adapt to the libraries you're using; don't clash with them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.