User Compilation Messages (C or C++)

Sometimes it’s useful to be able to get the compiler to emit custom messages, complete with location information so that clicking on the message will take you to the appropriate place in the sources much as error messages do.

To do this, add the following to some common header file:

// stringised version of line number (must be done in two steps)
#define STRINGISE(N) #N

// MSVC-suitable routines for formatting <#pragma message>
#define __LOC__ __FILE__ "(" __LINE_STR__ ")"
#define __OUTPUT_FORMAT__(type) __LOC__ " : " type " : "

// specific message types for <#pragma message>
#define __WARN__ __OUTPUT_FORMAT__("warning")
#define __ERR__ __OUTPUT_FORMAT__("error")
#define __MSG__ __OUTPUT_FORMAT__("programmer's message")
#define __TODO__ __OUTPUT_FORMAT__("to do")

You can add as many different message types as you like.

Then to generate a message, do e.g.:

#pragma message ( __MSG__ "my message" )

With MSVC, The <__ERR__> and <__WARN__> messages will contribute to the error and warning counts, respectively.

Not all compilers support <#pragma message>; MSVC does, G++ does not.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: