#pragma comment

May 26, 2009 at 1:44pm
closed account (S6k9GNh0)
As many of you know, #pragma is a compiler specific macro.

#pragma comment allows the user to send messages to the compiler at compile-time. My problem is that I've heard that this is a bad practice from a couple of people. And I can't really get evidence that it's worth using but I keep seeing it over and over. Are they're any real disadvantages to it?
May 26, 2009 at 1:57pm
Technically, #pragma's behavior is undefined.
http://en.wikipedia.org/wiki/Undefined_behavior
GCC 1.21, upon finding a #pragma directive, would instead attempt to launch commonly distributed Unix games such as NetHack and Rogue, or start Emacs running a simulation of the Towers of Hanoi.

The same rule applies as with everything that isn't portable. If you're absolutely sure your code will always be compiled with the same compiler, then there's no harm in using it. That's why VC++'s standard headers are littered with #pragma inclusion guards.
May 26, 2009 at 2:10pm
Yeah, it's not in the Standard. I'd like to use #pragma once in place of #ifndef XYZ #define XYZ ... #endif , but I avoid it. The include guards work everywhere properly.
May 26, 2009 at 2:47pm
Like most things in C/C++, it's there for a reason.

If you want to switch on or control certain compiler specific features, you often do this with pragmas and/or via compiler command line switches. Like everything else, it can be abused, but can be exactly what you need when used properly. Take record packing for example.

Recent GNU compilers complain about pragmas they don't understand. This is a good thing as different it's possible that a commonly named pragma may be implemented differently on different compilers. All pragmas should be wrapped in #ifdef statements.

To summarise, it's not wrong to use them. Use them minimally and wisely and always wrap them with #ifdef statements.
May 26, 2009 at 4:10pm
closed account (S6k9GNh0)
Thanks for the replies! Though I still have questions, mainly concerning why pragma isn't in the standard library but perhaps I can find other areas to search for this. I, of course, wouldn't mind it being answered here but for now I'll mark this as solved.
May 26, 2009 at 4:24pm
It's not in the standard libary because it communicates non-standard information to the compiler.
May 26, 2009 at 4:49pm
It's purposely undefined to let compiler vendors use it for whatever they want.
Topic archived. No new replies allowed.