Is there a portable way to print a message from the C preprocessor?

92,865

Solution 1

The warning directive is probably the closest you'll get, but it's not entirely platform-independent:

#warning "C Preprocessor got here!"

AFAIK this works on most compilers except MSVC, on which you'll have to use a pragma directive:

#pragma message ( "C Preprocessor got here!" )

Solution 2

The following are supported by MSVC, and GCC.

#pragma message("stuff")
#pragma message "stuff"

Clang has begun adding support recently, see here for more.

Solution 3

You might want to try: #pragma message("Hello World!")

Solution 4

Most C compilers will recognize a #warning directive, so

 #warning "Got here"

There's also the standard '#error' directive,

 #error "Got here"

While all compilers support that, it'll also stop the compilation/preprocessing.

Solution 5

#pragma message("foo")

works great. Also wouldn't stop compilation even if you use -Werror

Share:
92,865
Andrew Wagner
Author by

Andrew Wagner

I'm currently a Post-Doc researcher at the University of Leuven, Belgium working on launching systems for flying wind turbines.

Updated on January 10, 2020

Comments

  • Andrew Wagner
    Andrew Wagner over 4 years

    I would like to be able to do something like

    #print "C Preprocessor got here!"
    

    for debugging purposes. What's the best / most portable way to do this?

  • Bob Kaufman
    Bob Kaufman almost 14 years
    This won't print at compile-time, which is what I'm thinking OP is looking for.
  • Alexander Rafferty
    Alexander Rafferty almost 14 years
    I assumed he meant printing at run-time.
  • Bryan
    Bryan almost 14 years
    Which begs the question, can you put a directive based on a compilation flag to swap "pragma message" and "warning" somehow? For example, something like: #ifdef _LINUX #define #preprocmsg "#warning" else #define #preprocmsg "#pragma message"... I'll have to try that but instinct tells me the answer is no.
  • Andrew Wagner
    Andrew Wagner over 13 years
    I was asking about compile-time. Thanks!
  • Matt Joiner
    Matt Joiner over 13 years
    @Bryan: Yes. #define WARNING(msg) _Pragma("message " #msg)
  • maxschlepzig
    maxschlepzig about 11 years
    Just for the record, Solaris Studio 12.3 (Sun C 5.12) does not support this pragma.
  • Danny S
    Danny S over 9 years
    #pragma message () is not supported by older versions of gcc (such as gcc 4.1.2, the default version on RHEL5). I have yet to find an appropriate equivalent for these older versions - #warning is not going to be great, as warnings are treated as errors for us generally, and we'd really like the message to be informational, rather than stop the compilation.
  • Renan Gemignani
    Renan Gemignani over 7 years
    Issuing a warning is very inconvenient when your project compiles with -Wall by default. #pragma message doesn't have that problem.
  • save_jeff
    save_jeff over 4 years
    Works with Arduino 1.8 using Visual Studio vMicro. Thanks!
  • ead
    ead over 3 years
    Supported by clang at least since 3.0 and by gcc since 5.1 (and in a slightly different form since 4.4.7). Even if clang says "1 warning generated", it doesn't fail build when built with -Werror: gcc.godbolt.org/z/xoK6b8
  • Aaron Franke
    Aaron Franke over 3 years
    Is there a way to make this work with numbers or other values? It says it expects a string, but I would like to print out what a numeric #define is calculated to be.
  • jxramos
    jxramos almost 3 years
    is there someway to substitute in macro values, is that possible? Maybe something like #warning "DEBUG="DEBUG
  • Tzalumen
    Tzalumen over 2 years
    @AaronFranke During the preprocessor step, all defines should be ASCII.