views:

135

answers:

4

hello, sometimes i see that certain projects write something to the output during compilation.

how can that be achieved in MSVC++

thanks!

+6  A: 

You want to include something like this in your source code:

#pragma message("Hello World")
Timo Geusch
+10  A: 

use #pragma message e.g.

#define MESSAGE(t) message(__FILE__ "(" STRINGXXX(__LINE__) ") : " t)
#define STRINGXXX(x) STRINGYYY(x)
#define STRINGYYY(x) #x

then if you put

#pragma MESSAGE("TODO: testing")

it will appear as a clickable message just like the normal compiler messages

Steve Gilham
now That's a neat trick!
xtofl
Why STRINGXXX and STRINGYYY instead of just one macro?
rpg
__LINE__ is a number and it needs to be transformed into a string. You use another macro in order to avoid having the string "__LINE__" all over the place.
Cristian Adam
You can also use _CRT_STRINGIZE defined in crtdefs.h so you only need to define the macro MESSAGE
Stephen Nutt
+1  A: 

You can use #pragma message in one of your source files to output a string when that file is preprocessed.

Also, when a custom, pre- or post- build step is executed, the "description" field is echoed to standard output.

Nick Meyer
+1  A: 

As Timo Geusch said: the #pragma message directive is used for that.

As an exotic side effect of template metaprogramming, it's also possible to use the compiler as a calculator :)

template<int i> struct Message;

template<int i> struct Fac {
   static const int v = i * Fac< i-1 >::v; 
};

template<> struct Fac<1> { static const int v = 1; };

Message< Fac<10>::v > m;

will result in the output message

Line 10: error: aggregate 'Message<3628800> m' has incomplete type and cannot be defined
xtofl