Click here to Skip to main content
11,571,471 members (70,385 online)
Click here to Skip to main content

Message with style

, 22 Feb 2002 83.6K 361 32
Rate this:
Please Sign up or sign in to vote.
Add this simple #define to add compiler messages with location to your source code

VC's #pragma message

Ever tried to add the line number of a compile time message to your source code?

#pragma message(__FILE__ __LINE__ ": important part to be changed!!"

Won't work. Anyway, with some tricks you can do exactly this. The nice thing is that you will never ever again need to use "Find" to locate a particular message in your source code. Just double click the line with your message in the output window and VC's editor will jump to that location.

Here is what you need to add to your source code to make this work:

#define __STR2__(x) #x
#define __STR1__(x) __STR2__(x)
#define __LOC__ __FILE__ "("__STR1__(__LINE__)") : Warning Msg: "
#define __LOC2__ __FILE__ "("__STR1__(__LINE__)") : "

Now you can use the #pragma message to add the location of the message:

#pragma message(__LOC__ "important part to be changed")
#pragma message(__LOC2__ "error C9901: wish that error would exist")

The preceding pragmas will yield the following output from the compiler (preprocessor)

C:\code\pragma\test.cpp(33) : Warning Msg: important part to be changed
c:\code\pragma\test.cpp(34) : error C9901: wish that error would exist

Have fun with your new #pragma's...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
ThatsAlok1-Jan-12 23:26
memberThatsAlok1-Jan-12 23:26 
GeneralVery Useful! Pin
mattski23-Apr-07 18:29
membermattski23-Apr-07 18:29 
Generalc# Pin
Sk8tz23-Apr-07 2:53
memberSk8tz23-Apr-07 2:53 
AnswerRe: c# Pin
Andreas Saurwein Franci Gonalves24-Apr-07 6:23
memberAndreas Saurwein Franci Gonalves24-Apr-07 6:23 
GeneralTo display the warnings as warnings in the summary Pin
dEUs[CPP]9-Jun-03 11:40
sussdEUs[CPP]9-Jun-03 11:40 
GeneralRe: To display the warnings as warnings in the summary Pin
SaurweinAndreas9-Jun-03 21:22
memberSaurweinAndreas9-Jun-03 21:22 
GeneralJust an Append Pin
Diogo Quintela6-Dec-02 7:52
memberDiogo Quintela6-Dec-02 7:52 
GeneralRe: Just an Append Pin
Andreas Saurwein6-Dec-02 12:54
memberAndreas Saurwein6-Dec-02 12:54 
GeneralRe: Just an Append Pin
dEUs[CPP]7-Jun-03 23:30
sussdEUs[CPP]7-Jun-03 23:30 
No!
#error produces a fatal error and stops compiling, so you never see all your errors at once!
GeneralThanks! Very useful! Pin
Cathy10-Oct-02 10:42
memberCathy10-Oct-02 10:42 
GeneralRe: Thanks! Very useful! Pin
Andreas Saurwein10-Oct-02 12:55
memberAndreas Saurwein10-Oct-02 12:55 
GeneralUseless Stuff Pin
Guest13-Aug-02 3:01
sussGuest13-Aug-02 3:01 
GeneralRe: Useless Stuff Pin
Bamaco24-Jun-04 9:31
memberBamaco24-Jun-04 9:31 
GeneralRe: Useless Stuff Pin
troff8-Jan-05 21:36
membertroff8-Jan-05 21:36 
GeneralRe: Useless Stuff Pin
Bamaco28-Jan-05 22:33
memberBamaco28-Jan-05 22:33 
GeneralWhats this Junk Pin
Guest13-Aug-02 2:58
sussGuest13-Aug-02 2:58 
GeneralRe: Whats this Junk Pin
Anonymous25-Oct-02 3:47
sussAnonymous25-Oct-02 3:47 
GeneralRe: Whats this Junk Pin
Anonymous13-Aug-02 3:31
sussAnonymous13-Aug-02 3:31 
GeneralRe: Whats this Junk Pin
petervn8-Mar-06 14:08
memberpetervn8-Mar-06 14:08 
GeneralRe: Whats this Junk Pin
ThatsAlok1-Jan-12 23:25
memberThatsAlok1-Jan-12 23:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 23 Feb 2002
Article Copyright 2002 by Andreas S. Franci Gonçalves
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid