Tip: Macros to Generate Compile Time Messages

The Visual Studio compiler/IDE generates ‘clickable’ error messages in the compiler output. If an error occurs during compilation, you can click on that error message and be brought right to the offending line of source code.

The following macros provide a way of generating your own ‘clickable’ messages in your source code. There are two scenarios where I’ve find these types of messages to be very helpful:

  • When writing new code, it’s helpful to sprinkle the code with “To Do” notes for unfinished bits of code. By doing this, every time you compile your project you get a list of specific reminders of code that still needs work.
  • When you have multiple different type of builds of the same source code, it can be helpful at compile time to print out messages that describe details of this specific build.

The macros themselves are short and simple. Just add these to one of your project’s global header file.

#define PRAGMA_STR1(x)  #x
#define PRAGMA_STR2(x)  PRAGMA_STR1 (x)
#define NOTE(x)  message (__FILE__ "(" PRAGMA_STR2(__LINE__) ") :
   -NOTE- " #x)
#define NOTE_wARG(x,y)  message
   (__FILE__ "(" PRAGMA_STR2(__LINE__) ") :
   -NOTE- " #x PRAGMA_STR2(y))

If you add the following line of code somewhere in your code:

#pragma NOTE (TO DO - add error checking of inputs)

When you compile that code you will get a line of output like this:

.Subsystem.cpp(83) :
   -NOTE- TO DO - add error checking of inputs

The second macro is called NOTE_wARG. If you add the following line of code somewhere in your driver:

#pragma NOTE_wARG (Number of buffers=, NUM_COMM_BUFFERS)

When you compile that code, you will get a line of output like this:

.Subsystem.cpp(97) :    -NOTE- Number of buffers=16

With this NOTE_wARG macro, you can print out the compile time value of the numeric constant.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read