Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Beginner
Mar 2, 2007

Create #pragma Messages to Reach the Source of a Message

This macro is for use in #pragma messages. It allows you to double-click on an output message and automatically open the file in the editor at the source #pragma message statement:

#define STRING2(x) #x
#define STRING(x) STRING2(x)
#define FILE_LOC __FILE__ "(" STRING(__LINE___) ") : "

#pragma message(FILE_LOC "Your message here")
When compiling, this will generate output like:

c:\My Projects\MyFile.cpp(27) : Your message here
When you double-click on the message, MyFile.cpp will open to line 27. Note that this is IDE-dependent; it relies on mimicking the format that Visual Studio uses for build warnings and errors. I've used it in Visual Studio 6 and 2005; your mileage may vary in other IDEs.

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