views:

108

answers:

5

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?

A: 

You can't. Preprocessors are processed before the C code. There are no preprocessor directives to print to the screen, because preprocessor code isn't executed, it is used to generate the C code which will be compiled into executable code.

Anything wrong with:

#ifdef ...
printf("Hello");
#endif

Because this is all you can do as far as preprocessors go.

Alexander Rafferty
This won't print at compile-time, which is what I'm thinking OP is looking for.
Bob Kaufman
I assumed he meant printing at run-time.
Alexander Rafferty
I was asking about compile-time. Thanks!
Drew Wagner
+6  A: 

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
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
@Bryan: The best you can do is put `#ifdef _MSC_VER` `#pragma MESSAGE` `#else` `#warning MESSAGE` `#endif` into `warning.h`, then do the two-line `#define MESSAGE "message here"` `#include "warning.h"` when you want to output a message.
caf
+3  A: 

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

Ruel
+3  A: 

The following are supported by MSVC, and GCC.

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

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

Matt Joiner
+1  A: 

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