How do I print non-error messages during compilation?


Commenter Worf remarked, "My one wish is that #warning would be supported."

I always find it interesting when people say "I wish that Microsoft would stop following standards," since the #warning directive is nonstandard.

The Microsoft C/C++ compiler implements the feature in a method compatible with the standard, namely via a #pragma directive.

#pragma message("You really shouldn't be doing that.")

If you want to warn people away from deprecated functionality, you can use the #pragma deprecated() directive or the even more convenient (but more standards-troublesome) __declspec(deprecated) declaration specifier. The declaration specifier is much more convenient than the preprocessor directive because you can use it in a macro, and you can attach it to specific overloads of a function. (It's also more standards-troublesome because, while it is still permitted by the standard because it begins with a double-underscore, it is also not required to be ignored by compilers which do not understand it.)

In my experience, however, printing messages during compilation is of little consequence. Print all the messages during compilation as you want; nobody will read them. The only thing that gets attention is an actual warning or error. (And in many cases, only the error will get any attention at all.)

Comments (23)
  1. Cesar says:

    This is why you should enable the warnings-as-errors option (-Werror on gcc, I do not know what the corresponding parameter is for MSVC). This way, you cannot easily ignore a warning.

  2. Joshua says:

    Raymond's got half a point. Diagnostic messages during compile only get checked when debugging the build chain.

    The thing about __ markers is they are supposed to be defined in such a way that they can be dropped by an appropriate define (usually passed as -D, e.g. -D__declspec(x) ).

  3. QRS says:

    My one wish is that C99 would be supported.

  4. Martok says:

    Real Klingons don't use warnings, commander Worf!

  5. spork says:

    Print all the messages during compilation as you want; nobody will read them.

    Uh, maybe Worf will read them.

  6. Abram N says:

    so, Raymond – do YOU read the compiler messages?

  7. Random832 says:

    "I always find it interesting when people say "I wish that Microsoft would stop following standards," since the #warning directive is nonstandard."

    Implementing a feature not specified by the standard is not the same as not following the standard – implementing #warning has no impact on any program written in standard C or C++ [because they obviously won't use it]. And to say otherwise is to implicitly claim that competing compilers that do implement it aren't following the standard.

    "The only thing that gets attention is an actual warning or error." And yet it's so mystifying that people want to be able to generate an "actual warning" rather than just a message?

    "it is also not required to be ignored by compilers which do not understand it" – perhaps, but compilers are not required to understand non-standard pragmas _in the same way_. "#pragma message" could mean _anything_ on some other compiler, so it still has to be guarded by making sure you know what implementation you're on. And being ignored is the wrong thing: at least #warning is a syntax error and therefore a required diagnostic.

    [If the Microsoft compiler accepted #earning, then it would be in violation of the part if the standard that requires that #warning generate an error. -Raymond]
  8. No One says:

    @Abram N: Raymond doesn't need to read them — he psychically knows what they will be before he compiles.

  9. SimonRev says:

    One approach to generate a warning is the following

    #define WARNNAME2(x,y) x##y

    #define WARNNAME1(x,y) WARNNAME2(x,y)

    #define WARNNAME(x) WARNNAME1(x,__COUNTER__)

    #define WARNING(text)

    struct WARNNAME(warn) {

       __declspec(deprecated(text)) void f() {}

       void g() { f(); }

    };

    // Generate a warning

    WARNING("This is a scary looking warning")

    This generates the warning via the deprecated approach, which is hardly optimal for most warnings I need to generate, but is the only workable approach I have found.

    I do think that there are valid reasons to allow a user to generate a custom warning, possibly via something like #pragma userwarning("Danger"), or even the non-standards compliant #warning which at least has the advantage of being compatible with GCC, and possibly becoming a de facto standard.

    @Random — about your question: "And yet it's so mystifying that people want to be able to generate an "actual warning" rather than just a message?", the blog post as originally posted this morning had a technical inaccuracy in it which has since been corrected.  During the period the technical inaccuracy was present, Raymond's statement made sense.  Now it does seem puzzling and a bit out of place.

    [__pragma is probably easier. -Raymond]
  10. cfgauss says:

    @Abram, did you just ask if the guy who wears a suit to work every day, and still codes, despite his withered hand, reads compiler warnings?

  11. 640k says:

    This is NOT a technical decision. It's a BUSINESS decision. Other compilers supports it, therefore MS cannot.

  12. Jon D says:

    One useful trick to generate a message *and* a warning in VS2005 (and likely later): put a semicolon after the #pragma message.  So:

    #pragma message("manah manah");

  13. Mike says:

    SimonRev,

    This is probably "simpler"

    #define STRINGIZE2__(x) #x

    #define STRINGIZE1__(x) STRINGIZE2__(x)

    #define LOC__ FILE "("STRINGIZE1__(LINE)") : warning Msg: "

    #define WARNING(MSG) pragma(message(LOC MSG))

    WARNING("hi");

    results in:

    file.cpp(649): warning Msg: hi

    Avoids the deprecated issue, is still clickable and shows up in the error list in the IDE, and is colorized by vcbuild/msbuild.

    blogs.msdn.com/…/msbuild-visual-studio-aware-error-messages-and-message-formats.aspx

    At least works in VS2008 and VS2010, but I don't know about earlier or later versions.

  14. Anonymous Coward says:

    I use GCC with almost all warnings as errors. Since without getting rid of the errors the project won't build, I necessarily have to read the things. Of course, the warnings that are still warnings I ignore because they're silly.

  15. Mike says:

    And I see that the technique was probably copied verbatim from one of the comments from the post Raymond linked, so I feel a bit silly now.

  16. Leo Davidson says:

    Maybe Worf's wish was that #warning would be supported *by both the C++ standard and the compiler*?

    It's not like the C++ standard is set in stone and cannot be influenced. If we all pray for this feature each night then it might be part of C++0MG in time for our great-great-grandchildren's seventieth birthdays.

  17. Neil says:

    Firefox developers keep trying to turn on warnings-as-errors but the problem is that newer compilers have more warnings… perhaps there should be a cut-off point, so that you only error for a known set of warnings.

  18. ymett says:

    [If the Microsoft compiler accepted #warning, then it would be in violation of the part if the standard that requires that #warning generate an error. -Raymond]

    Technically speaking, the standard never requires more than a 'diagnostic', which the compiler does actually generate – ie the warning.

  19. Random832 says:

    @ymett actually, for the standards that define #error, they do in fact require that the implementation "fail to translate" upon encountering it. But in no other circumstance do they require this.

  20. cheong00 says:

    Actually, I work hard to clear all warnings from the source code, and mark "surpress" for places where I think the warning can be safely ignored (like those generic related ones.) when I worked on Java projects in my previous job. In my current job when is on C# again, I'll not leave any warnings behind.

    Sometimes warning can tell you a potential bug before they come to bite you, so I'll make sure they're all assessed and either fixed or surpressed as okay.

  21. cheong00 says:

    Print all the messages during compilation as you want; nobody will read them.

    Actually even in the days of WinXP where we only have 3 logs that can contain events, I use filtering to read all warnings and errors in order to make sure systems function properly.

    In Vista or later, they encourage splitting logs to different container, and that's even more convenient.

  22. barbie says:

    Ah, but #pragma message does not generate a warning. It's not listed in the IDE as a warning when looking for warnings and errors. So it's indeed worthless because people do not go sift through the basic compiler output. But I've seen many sift through the processed output (the warnings/errors list). That's why you need a #warning (a #pragma warning would be fine too). But message is not enough.

  23. Tim says:

    @barbie: Actually, it does generate a warning in the IDE's Error List as long as the message contains the phrase "warning:". Likewise, if you write #pragma message( "error: Don't do that." ) it will produce an error and compilation will fail.

Comments are closed.

Skip to main content