Tip: Macros to Generate Compile Time Messages | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 4, 2008
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.