Search code examples
cmacrospragma

Insert double quotes in a macro for pragma in C


I'm trying to create a macro in C in order to create the correct pragma declaration.

_pragma(section .BLOCK1) //Correct but deprecated
_pragma(section ".BLOCK1") //Correct without warning

Following code is working, but the compiler gives me a warning (deprecated declaration):

#define DO_PRAGMA(x) _Pragma(#x)

#define PRAGMA(number) \
DO_PRAGMA(section .BLOCK##number)

PRAGMA(1)

How I can include the double quotes in the macro? I have already tried inserting "\"", but it is not working because the string is interpreted directly.


Solution

  • You can pass this to a helper macro which expands and stringifies the arguments.

    #define _stringify(_x)  #_x
    
    #define DO_PRAGMA(a) _Pragma(_stringify(a))
    
    #define PRAGMA(number) \
        DO_PRAGMA(section _stringify(.BLOCK##number))