c++gccpragma

What is the equivalent in GCC to MSVC's '#pragma message'?


On MSVC I can issue compilation-time message like so:

#pragma message("...some compilation-time message...")

Is there an equivalent in GCC?


Solution

  • You can just use #pragma in gcc too. Otherwise there is warning and error:

    #pragma message("...some compilation-time message...")
    #warning I warn you.
    #error I mean it.
    

    results in

    $ g++ -O2 -W -Wall -g -c foo.cc
    foo.cc:2:2: warning: #warning I warn you. [-Wcpp]
        2 | #warning I warn you.
          |  ^~~~~~~
    foo.cc:3:2: error: #error I mean it.
        3 | #error I mean it.
          |  ^~~~~
    foo.cc:1:54: note: ‘#pragma message: ...some compilation-time message...’
        1 | #pragma message("...some compilation-time message...")
          |                                                      ^