Thursday, 2 July 2009

C++: How to say "#warning" to the Visual Studio C++ compiler

I encountered a piece of code that would no longer compile in a particular build variant of our product. I wanted to hack it out, and leave a compiler warning in it's place so we wouldn't lose track of the change.

It's easy in gcc. You simply say:
#warning FIXME: Code removed because...
So that's me sorted for Linux and MacOS. I'm happy in the Fun Place.

But in the Dark Place I was clueless. How do you say #warning to Visual Studio? You can happily write #warning in C#, but not C++.

Interestingly, the answer fell below my Google/Boredom Threshold (i.e. a web search didn't reveal the answer in sufficient few clicks that I lost interest). I just shoved in a run-time assertion instead. It'd do the job, but not as immediately as I would have liked.

Thanks to hashpling and the miracle that is Twitter, I now know the answer, and share it with you in the vein hope it might come higher up the Google rankings for those poor souls that follow me:
#pragma message ("FIXME: Code removed because...")
Needless to say, this is all tediously non-standard.

For bonus points

This still doesn't get us exactly the same behaviour as gcc's #warning. The message is produced, but without file and line information. This means that if you double-click the message in the VS IDE it will not jump to the warning in the editor window. It also means that build logs aren't much use.

Sam Saariste pointed this out, and here's the standard preprocessor mumbo-jumbo you have to jump through to get the exact warning behaviour I was after:
#define STRINGIZE_HELPER(x) #x
#define STRINGIZE(x) STRINGIZE_HELPER(x)
#define WARNING(desc) message(__FILE__ "(" STRINGIZE(__LINE__) ") : Warning: " #desc)

// usage:
#pragma WARNING(FIXME: Code removed because...)
Couldn't be simpler, could it?!

22 comments:

codemonkey_uk said...

It's a shame you can't #define a #pragma, or you could take this to it's logical conclusion:

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define WARNING( txt ) __FILE__"(" TOSTRING(__LINE__) ") : warning: " txt

#pragma message( WARNING( "a warning you can click on" ) )

Pete Goodliffe said...

To be honest, it's a shame there's still no standard way of doing this.

#warning seems quite sensible to me...

James said...

So the obvious question is: Why do that?

I think it's a mistake to use compiler output as your bug tracking tool. We've all seen noisy builds that spew so many diagnostics that new problems are lost in the wash. Either the problem's important enough to fix now, or it should go in the bug database...

Pete Goodliffe said...

James,

Fair point. My response is simple: "yes" and "no".

My suggestion is not using compiler output as bug tracking. Of course, you have bug tracking to track bugs. I'm using it to mark a section of code I'd like to come back to shortly in a very obvious way, and as a tool to communicate to other programmers - in the code itself (the only definite place where you'll see comments ABOUT the code as you work on it) - that this is an area under work right now that is not "production" ready.

This is the kind of thing we do with comments all the time, but #warning is a construct that allows us to say it with a little more vehemence!

*If* you have an awful, noisy build then of course #warnings would get lost in the noise. But in a nice, quiet build, this stands out well.

It's a matter of being pragmatic, I guess.

Certainly, in many (perhaps not-so-well-run) dev shops, putting the issue into the bug database is just a different way of throwing the problem away and forgetting about it, lost in the noise of 1,000,000 other bugs :-)

AldurDisciple said...

An interesting note for future readers of this article:

if you use the set of macro given in section "For bonus points", you might prefer using this macro instead
#define WARNING(desc) message(__FILE__ "(" STRINGIZE(__LINE__) ") : warning: " #desc)
(notice the lowercase 'w' in 'warning'), because in this case VisualStudio will automatically display the warning in the "Error List" window, so that it don't get lost in the "Output" window.

Friedel said...

@James Sometimes you want to commit a change or some test code to investigate things.
I use this a lot (once every 6 month) when I need a define from someone else, e.g. from i13n team.
with each local #define i post a #warning "define missing"

Anonymous said...

@James
As others have said, this is for something that needs attention before release, but must not be forgotten or prevent a build.

It works especially well if you have the rule that dictates you can't release before resolving >all< warnings.

erzet said...

I don't often defend the way msvc treats the standard, but in this case, they're the ones doing it properly and #warning is the "tediously non-standard" way.

The standard has a section on preprocessor directives (16) and while #error is mentioned, #warning is not. What's more, compilers are required to ignore a pragma they don't recognize but error out on a directive they don't know.

So if a compiler wants to provide functionality the standard doesn't mention, it should be done through pragmas, not by inventing a new directive (as pretty as it may seem).

sandeep saxena said...


I love this!!The blog is very nice to me. Im always keeping this idea in mind. I appreciate for your help once again.
JMeter Training in Chennai
JMeter Training Course
JMeter Training in Adyar
JMeter Training in Porur
Appium Training in Chennai
javascript training in chennai
core java training in chennai
C C++ Training in Chennai

Anbarasan14 said...

I am greatly thankful to the blog admin for the interest in sharing this information in detail with us.

IELTS Classes in Mumbai
IELTS Coaching in Mumbai
IELTS Mumbai
IELTS Center in Mumbai
Best IELTS Coaching in Mumbai
Spoken English Classes in Chennai
IELTS Coaching in Chennai
English Speaking Classes in Mumbai

sandhiya said...

Awesome blog with great piece of information. Very well written blog with crisp and neat content. Keep sharing more such blogs.
Cloud Computing Training in Chennai
Cloud Training in Chennai
Data Science Course in Chennai
Azure courses in Chennai
VMware course
R Programming Training in Chennai
Cloud Certification in Chennai

Anonymous said...

Using Visual Studio 2019 a double-click on the message will no longer jump to the given line. Reason why is that Microsoft changed the format of the prefix. It now includes the column as well. Thus, the macro must produce a column number.

// version for Visual Studio 2019
#define WARNING( txt ) __FILE__"(" TOSTRING(__LINE__) ",1) : warning: " txt

Column 1 places the cursor at the beginning of the line. Currently, column 0 does work as well. That would distinguish the user-defined message from other messages but may fail in the future.

Jagna Co Kalani said...

Great Article. Thank you for sharing! Really an awesome post for every one.
Project Centers in Chennai

JavaScript Training in Chennai

Final Year Project Domains for IT

JavaScript Training in Chennai

Chris Hemsworth said...

The article is so informative. This is more helpful for our
selenium training in chennai
selenium online courses best selenium online training
selenium testing training
selenium classes
Thanks for sharing.

zuan said...

Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
Web Designing Training Institute in Chennai | web design training class in chennai | web designing course in chennai with placement
Mobile Application Development Courses in chennai
Data Science Training in Chennai | Data Science courses in Chennai
Professional packers and movers in chennai | PDY Packers | Household Goods Shifting
Web Designing Training Institute in Chennai | Web Designing courses in Chennai
Google ads services | Google Ads Management agency
Web Designing Course in Chennai | Web Designing Training in Chennai

priya said...

I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective.
php online training in chennai
php programming center in chennai
php class in chennnai
php certification course
php developer training institution chennai
php training in chennnai
php mysql course in chennai
php institute in chennnai
php course in chennnai
php training with placement in chennnai
php developer course

priya said...

Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
appium online training
appium training centres in chennai
best appium training institute in chennnai
apppium course
mobile appium in chennnai
mobile training in chennnai
appium training institute in chennnai

Unknown said...

Thanks for the interesting blog that you have implemented here. Very helpful and innovative. Waiting for your next upcoming article.
Java training in chennai
Java training institute in chennai
Java course in chennai
Java training classes
Java training
Java programming classes
core java coure
<a

w3webschool said...

Nice blog, it’s so knowledgeable, informative, and good looking site. I appreciate your hard work. Good job. Thank you for this wonderful sharing with us. Keep Sharing.
Digital Marketing Course In Kolkata
Web Design Course In Kolkata

Anurag Srivastava said...

This is really too useful and have more ideas and keep sharing many techniques...you can explore the many exciting Job Opportunities on India Postal Recruitment 2020. Latest and also upcoming Notifications on India Post recruitment are updated in this page immediately...

Pooja said...

Great Article. Thanks for Sharing This Freejobalert article.

Mithun said...

Thanks for the informative article About Java. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery