131

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Andrew Wagner
  • 22,677
  • 21
  • 86
  • 100

6 Answers6

154

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!" )
You
  • 22,800
  • 3
  • 51
  • 64
  • 5
    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. – Bryan Sep 30 '10 at 00:41
  • 12
    @Bryan: Yes. `#define WARNING(msg) _Pragma("message " #msg)` – Matt Joiner Mar 02 '11 at 07:31
  • 1
    #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. – Danny S Mar 23 '15 at 04:10
  • 9
    Issuing a warning is very inconvenient when your project compiles with -Wall by default. #pragma message doesn't have that problem. – Renan Gemignani Feb 21 '17 at 14:49
  • is there someway to substitute in macro values, is that possible? Maybe something like `#warning "DEBUG="DEBUG` – jxramos Jul 08 '21 at 23:11
  • @Bryan #if defined(_WIN32) #pragma message ( "embrace, extend, extinguish" ) #else #warning your message here #endif – vesperto Jan 02 '23 at 17:50
67

The following are supported by MSVC, and GCC.

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

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

Community
  • 1
  • 1
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • 3
    Just for the record, Solaris Studio 12.3 (Sun C 5.12) does not support this pragma. – maxschlepzig May 28 '13 at 13:06
  • Works with Arduino 1.8 using Visual Studio vMicro. Thanks! – save_jeff Mar 29 '20 at 07:35
  • 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`: https://gcc.godbolt.org/z/xoK6b8 – ead Nov 21 '20 at 23:24
  • If parentheses are omitted with MSVC, you will end up getting a compile warning instead of a preprocessor message. Both work for GCC, but parentheses can be omitted for GCC. – phetdam Sep 11 '22 at 23:19
13

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

Ruel
  • 15,438
  • 7
  • 38
  • 49
12

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.

nos
  • 223,662
  • 58
  • 417
  • 506
10
#pragma message("foo")

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

Martin
  • 3,509
  • 3
  • 26
  • 31
  • 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. – Aaron Franke Jan 22 '21 at 10:28
  • @AaronFranke During the preprocessor step, all defines should be ASCII. – Tzalumen Mar 22 '22 at 21:19
0

Another solution is to use comments plus a shell script to process them. This takes some discipline (or a shell script which catches typos).

For example, I add comments formatted //TODO and then a shell script which collects all of them into a report.

For more complex use cases, you can try to write your own simple preprocessor. For example, you could edit your sources as *.c2 files. The simple preprocessor would read the source, look for //TODO, and write printf("TODO ...") into the output *.c file.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820