Tell me, is it possible to define a macro in such a way that, depending on whether it is used or not, it takes on different values?

Example:

#ifndef DISABLE_MANGLING # define DISABLE_MANGLING \ extern "C" { #else # undef DISABLE_MANGLING # define DISABLE_MANGLING \ } #endif 

Expectations : using a macro for the first time - it is revealed in extern "C" { , the second time - is revealed in } .

Reality : in both cases revealed in extern "C" { .

What tell me?

    1 answer 1

    How do you want to fail, because the preprocessor will not execute a previously written #ifdef encountering a macro in the text.

    Theoretically, you can write something like that (I reduced DISABLE_MANGLING to DM )

     #ifndef DM #define DM extern "C" { #define DME 1 #else #undef DM #if DME == 1 #undef DME #define DME 2 #define DM } #else #undef DME #define DME 1 #define DM extern "C" { #endif #endif DM 1 #if DME == 1 #undef DME #undef DM #define DME 2 #define DM } #else #undef DME #undef DM #define DME 1 #define DM extern "C" { #endif DM 2 #if DME == 1 #undef DME #undef DM #define DME 2 #define DM } #else #undef DME #undef DM #define DME 1 #define DM extern "C" { #endif DM 3 #if DME == 1 #undef DME #undef DM #define DME 2 #define DM } #else #undef DME #undef DM #define DME 1 #define DM extern "C" { #endif DM 4 

    and get as a result

     avp@avp-ubu1:hashcode$ gcc -E dm.c # 1 "dm.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 1 "<command-line>" 2 # 1 "dm.c" # 18 "dm.c" extern "C" { 1 # 32 "dm.c" } 2 # 45 "dm.c" extern "C" { 3 # 58 "dm.c" } 4 avp@avp-ubu1:hashcode$ 

    but I don’t think that it can be of any use in practice.

    Also, since the text resulting from the macro expansion is not re-analyzed by the preprocessor, such an attempt to reduce the amount of code

     #define XX \ #if DME == 1 \ #undef DME \ #undef DM \ #define DME 2 \ #define DM } \ #else \ #undef DME \ #undef DM \ #define DME 1 \ #define DM extern "C" { \ #endif DM 1 XX 

    will not work.

    In principle (if you really want to) you can reduce the amount of text by writing #ifdef to a separate file.

     // dm.h file #ifndef DM #define DM extern "C" { #define DME 1 #else #undef DM #if DME == 1 #undef DME #define DME 2 #define DM } #else #undef DME #define DME 1 #define DM extern "C" { #endif #endif 

    and including it in the "main code" like this

     // dm.c #include "dm.h" DM 1 #include "dm.h" DM 2 #include "dm.h" DM 3 #include "dm.h" DM 4