What does #pragma comment(lib, “XXX”) actually do with “XXX”?

That pragma is used to link against the specified .lib file. This is an alternative to specifying the library in the external dependencies field in project settings. Mostly, it’s used to support different versions: #ifdef USE_FIRST_VERSION #pragma comment(lib, “vers1.lib”) #else #pragma comment(lib, “vers2.lib”) #endif When your application uses a dynamically-linked library, a lib file tells … Read more

#pragma once position: before or after #include’s

#pragma once should be placed before any headers are included. Argument of #pragma directive is a subject to macro expansion. So content of included headers can alter the pragma behavior: // whatever.hpp … #define once lol_no // your_header.hpp #include “whatever.hpp” #pragma once // warning C4068: unknown pragma

Should I still use #include guards AND #pragma once?

It depends on how much portable your program is expected to be. As long as you are writing a program which is supposed to work with compilers which you know definitely support #prama once, just using #pragma once should suffice. But doing so you restrict your program to set of compilers which support the implementation … Read more

GCC does not honor ‘pragma GCC diagnostic’ to silence warnings [duplicate]

This appears to be a bug in gcc at least. The following code: #pragma GCC diagnostic ignored “-Wunknown-pragmas” #pragma GCC diagnostic ignored “-Wuninitialized” int fn(void) { #pragma xyzzy int x; return x; } int main (void) { return fn(); } has no problems ignoring the uninitialised x value but still complains about the pragma (without … Read more

#pragma warning disable & restore

At the very least you should be specific about which warnings you’ve deliberately chosen to ignore. That way, if later maintenance introduces a ‘new’ warning/issue that you should be made aware of, the warning about the newly-introduced error won’t be suppressed by your blanket pragma warning disable directive. You can get the warning numbers pertaining … Read more

How can I use #pragma message() so that the message points to the file(lineno)?

Here is one that allows you to click on the output pane: (There are also some other nice tips there) http://www.highprogrammer.com/alan/windev/visualstudio.html // Statements like: // #pragma message(Reminder “Fix this problem!”) // Which will cause messages like: // C:\Source\Project\main.cpp(47): Reminder: Fix this problem! // to show up during compiles. Note that you can NOT use the … Read more