Click here to Skip to main content
Click here to Skip to main content

Message with style

By , 22 Feb 2002
 

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

About the Author

Andreas Saurwein Franci Gonçalves
CEO Uniwares Ltda.
Brazil Brazil
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberThatsAlok1 Jan '12 - 23:26 
this article deserve it.. though i am reading it quite late Smile | :)
GeneralVery Useful!membermattski23 Apr '07 - 18:29 
This makes my TODO messages so much easier to find. Big Grin | :-D Thank you!
Generalc#memberSk8tz23 Apr '07 - 2:53 
Any idea how to do this in c#
 
Sk8tZ

AnswerRe: c#memberAndreas Saurwein Franci Gonalves24 Apr '07 - 6:23 
Well, you dont do that in C#.
To quote the C# reference: Unlike C and C++ directives, you cannot use these directives to create macros.
They do not expand therefore you cant create the desired effect. Anyway you can use the #warning pragma to output information with linenumbers.

 

Rufus[^] - Mailing List Management

GeneralTo display the warnings as warnings in the summarysussdEUs[CPP]9 Jun '03 - 11:40 
Hi folks,
if you change "Warning Msg:" to "warning:" at least VC++.NET displays them then as warnings in its compilesummary.
GeneralRe: To display the warnings as warnings in the summarymemberSaurweinAndreas9 Jun '03 - 21:22 
VC6 too.
 

Off to Brazil in a few days
GeneralJust an AppendmemberDiogo Quintela6 Dec '02 - 7:52 
-----------------------------------------------------------------
#define __STR2__(x) #x
#define __STR1__(x) __STR2__(x)
#define __LOC__ __FILE__ "("__STR1__(__LINE__)") : Warning Msg: "
#define __LOC2__ __FILE__ "("__STR1__(__LINE__)") : "
 
#define COMPILE_WARN(s) message(__LOC__ s)
#define COMPILE_ERROR(d,s) message(__LOC2__ "error " d ": " s)
-----------------------------------------------------------------
 
And know use only
#pragma COMPILE_WARN("Check it here")
in your code..
or
#pragma COMPILE_ERROR("C9999","Don't forget to code here!")
 


 
Diogo Quintela
GeneralRe: Just an AppendmemberAndreas Saurwein6 Dec '02 - 12:54 
You can do just that with #error Don't forget to code here! too.
 

I don't think this is a serious possesion, and the evil most likely comes from your hand. Colin J Davies, The Lounge


GeneralRe: Just an AppendsussdEUs[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!memberCathy10 Oct '02 - 10:42 
I hate it when I accidently leave debugging stuff in the code. This will help.
 
Cathy
 
Life's uncertain, have dessert first!

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

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