Concatenate strings in a macro using gfortran

后端 未结 1 815
北荒
北荒 2020-12-04 00:13

The C preprocessor macro for concatenation (##) does not seem to work on a Mac using gfortran. Using other Fortran compilers on other systems works so I am look

1条回答
  •  -上瘾入骨i
    2020-12-04 00:57

    ## doesn't work in gfortran (any OS, not just Mac) because it runs CPP in the traditional mode.

    According to this thread the gfortran mailing list the correct operator in the traditional mode is x/**/y, so you must distinguish between different compilers:

    #ifdef __GFORTRAN__
    #define CONCAT(x,y) x/**/y
    #else
    #define CONCAT(x,y) x ## y
    #endif
    

    Others (http://c-faq.com/cpp/oldpaste.html) use this form, which behaves better when a macro passed to the CONCAT (via Concatenating an expanded macro and a word using the Fortran preprocessor):

    #ifdef __GFORTRAN__
    #define PASTE(a) a
    #define CONCAT(a,b) PASTE(a)b
    #else
    #define PASTE(a) a ## b
    #define CONCAT(a,b) PASTE(a,b)
    #endif
    

    The indirect formulation helps to expand the passed macro before the strings are concatenated (it is too late after).

    0 讨论(0)
提交回复
热议问题