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.



Comments

  • DoesnB4t work for me (VS Studio 2003)

    Posted by GNiewerth on 03/04/2008 07:50am

    There are \ missing for multiple macro definitions lines, even after I added them I get an "Unknown pragma" warning.
    
    #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))
    
    #include "stdafx.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    #pragma NOTE ( "Hello World" )
    	return 0;
    }

    • Here's the Problem

      Posted by JerReilly on 03/05/2008 02:55pm

      The problem with your test app is that you need to move the #include "stdafx.h" up above the #define's and it will all work fine. Thanks for your interest and comments

      Reply
    • I see one issue

      Posted by JerReilly on 03/04/2008 10:04am

      Sorry you had trouble with this. I see two issues. The first one you already found. When I submitted the code it looks like I forgot to check the CodeGuru "Pre-formatted" box, so the code as written needs some line continuation characters (\). It looks from your note that you took care of that. The other issue is in your code when you used the #pragma NOTE you enclosed your strings in quotes. That's not correct and frankly I'm not sure what that what would do. I'll try it later today when I'm at a computer with a C compiler and I'll get back to you. In any case though, your test case should read #pragma NOTE ( Hello World ) rather than #pragma NOTE ( "Hello world" )

      Reply
    Reply
  • Useful tip

    Posted by kirants on 02/10/2008 01:11am

    .. neat and handy

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date