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.


  • Fake Oakley Radarlock Path online store

    Posted by wvlagyvrk on 07/01/2013 12:45am

    Fake Ray Bans ,Oakley can be a technique crystallization of high-tech and intelligent, setting up a combination of high-performance fashion eyewear. Typical Oakley style, striking colors and ultra-sound lenses and frames will be the ultimate expression of style, let him step up! fake oakley sunglasses ,Oakley sunglasses defensive glasses varieties use a the CLOS tendency to avoid the attention on the area, prevent stamping eye particles, water, or substance. Choice problems, you can get benefits. Choose the specific type of sunglasses to settle on a model, it is suitable for the actual required amount of your taste and personality. Oakley Juliet ,If you wish to choose a better view, to counterbalance the Oakley sunglasses within an open and clear. Although Oakley sunglasses optical brand choice, we recommend the make of Oakley sunglasses. I believe that men and women can't give can really buy fashion, searching for different grades of Oakley sunglasses from the rights of individuals total well being. To learn how the repair off good sunglasses can not only extend the duration people of sunglasses, and also can better be sure that the sunglasses eye protection role. Each Oakley shade to produce personal fun. Services are supplied through the requirements of men and some women, Oakley glasses extremely exciting choice. The main and innovative style, code and data bunch of posters from the 2012 sunglasses summer preparation, really the only products which would your going away the most beneficial partner? The grade of Oakley sunglasses (especially between the kind) to offer substantial protection and the eyes in the sun's UV, and so suitable for people of any age. Oakley glasses, the innovation has been brought numerous new service technologies, and constantly challenged the standard wisdom. Today's gradient with Oakley sunglasses for women: they supply 100% Ultraviolet shield, luxury and ambitions do understand. Oakley sunglasses usually are engaged with the current economic consumer interest on shops needs to be arrive at involve the highest quality Oakley sunglasses credible, reliable suppliers to supply basic work. If you would like wholesale sunglasses online purchase is an excellent choice. That is a great way to compare different sunglasses give to them the cost of their articles, their brand of wholesalers and retailers.

  • 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

    • 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" )

  • Useful tip

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

    .. neat and handy

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

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

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