Q
Q
q2zoff2020-06-22 01:47:57
C++ / C#
q2zoff, 2020-06-22 01:47:57

How to set C preprocessor behavior in GCC?

Hello.

There is some code:

#ifdef func
#define temp func
#undef func
#endif

// тут выполняется какой-то код

#ifdef temp
#define func temp
#undef temp
#endif


Its meaning is simple: cancel the action of the macro in a certain part of the code, and after it expires, restore this macro.

The problem is that on one machine GCC works as expected, and on another, func is expanded literally to temp, and a bunch of undefined function temp warnings are printed to stderr.

Is there a way to hardcode the behavior of the preprocessor so that the second definition of func matches the value temp rather than the macro name (temp). Perhaps some compilation flags are responsible for this behavior?

Thanks in advance for the replies.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-06-22
@q27off

Its meaning is simple: cancel the action of the macro in a certain part of the code, and after it expires, restore this macro.

Thus, this cannot be done. Because in the place where the #define temp funcvalue of the func macro is written, it is not substituted. The word literally gets into temp func. After #undef functhe contents of the func macro will be lost. This behavior is prescribed by the standard, I don't know any compiler options that could change it. See eelis.net/c++draft/cpp.replace#10 and eelis.net/c++draft/cpp.rescan
But with one more level of indirection, you can make it work. For example:
#define foo bar
#define func foo
...
#undef func
...
#define func foo

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question