What Does "#Pragma Comment" Mean

What does #pragma comment mean?

#pragma comment is a compiler directive which indicates Visual C++ to leave a comment in the generated object file. The comment can then be read by the linker when it processes object files.

#pragma comment(lib, libname) tells the linker to add the 'libname' library to the list of library dependencies, as if you had added it in the project properties at Linker->Input->Additional dependencies

See #pragma comment on MSDN

C++: What does #pragma comment(lib, XXX ) actually do with XXX ?

If a program has this pragma it will look for the library OtherLib700.lib. If that is an import library when the program is loaded windows will search for OtherLib700.dll in the path. It will not try to look for OtherLib900.dll during execution so it must be finding your dll in a different folder. This assumes that OtherLib700.lib is an import library and not a static library. If OtherLib700.lib is a static library then that is all it needs.

Purpose of #pragma comment(user) in Visual and #pragma Comment in gcc

You seem to have misread the description of this #pragma. Microsoft's documentation says:

Places a general comment in the object file. The commentstring parameter contains the text of the comment. This comment record is ignored by the linker.

At least according to this documentation, it would appear that (since the record is ignored by the linker) such a comment would only embed the string into the object file, not the executable.

The obvious purpose would be to embed something like a copyright string into an object file being shipped as a library for developers to use. Such libraries are typically licensed to allow a developer to link the object files from the library into an executable, but not to redistribute the object files themselves.

In such a case, the effect would be that if the developer linked the object file to an executable, the copyright notice would disappear (and it's up to the developer to provide attribution if the license requires it), but if they re-distribute the object files, the original copyright notice would remain intact, more or less like a digital watermark.

Pragma comment C++

They are not.

The said pragma adds a defaultlib. The option a mandatory lib to the linker line.

The latter is processed no matter what, and you get error if it is missing.
The default lib is ignored silently if not found. And using options "ignore default libs" or "ignore specific default lib" can be used to dismiss its use even if it is present.

C++ Visual Studio: linking using pragma comment

The library writer can place a #pragma comment(lib, ...) command in the public header (.h) file. In this case, the client doesn't need to add this library to the linker dependencies list. By including an h-file in the program, the client is automatically linked to the required library.

What code have you written with #pragma you found useful?

Every pragma has its uses, or they wouldn't be there in the first place.

pragma "once" is simply less typing and tidier, if you know you won't be porting the code to a different compiler. It should be more efficient as well, as the compiler will not need to parse the header at all to determine whether or not to include its contents.

edit: To answer the comments: imagine you have a 200kB header file. With "once", the compiler loads this once and then knows that it does not need to include the header at all the next time it sees it referenced. With #if it has to load and parse the entire file every time to determine that all of the code is disabled by the if, because the if must be evaluated each time. On a large codebase this could make a significant difference, although in practical terms (especially with precompiled headers) it may not.

pragma "pack" is invaluable when you need binary compatibility for structs.

Edit: For binary formats, the bytes you supply must exactly match the required format - if your compiler adds some padding, it will screw up the data alignment and corrupt the data. So for serialisation to a binary file format or an in-memory structure that you wish to pass to/from an OS call or a TCP packet, using a struct that maps directly to the binary format is much more efficient than 'memberwise serialisation' (writing the fields one by one) - it uses less code and runs much faster (essential in embedded applications, even today).

pragma "error" and "message" are very handy, especially inside conditional compliation blocks (e.g. "error: The 'Release for ePhone' build is unimplemented", message: "extra debugging and profiling code is enabled in this build")

pragma "warning" (especially with push & pop) is very useful for temporarily disabling annoying warnings, especially when including poorly written third-party headers (that are full of warnings) - especially if you build with warning level 4.

edit: Good practice is to achieve zero warnings in the build so that when a warning occurs you notice it and fix it immediately. You should of course fix all warnings in your own code. However, some warnings simply cannot be fixed, and do not tell you anything important. Additionally, when using third party libraries, where you cannot change their code to fix the warnings, you can remove the 'spam' from your builds by disabling the library's warnings. Using push/pop allows you to selectively disable the warnings only during the library includes, so that your own code is still checked by the compiler.

Pragma ignoring comment [-Werror=unknown-pragmas]

"This pragma to link libraries from C++ source code is only supported by MSVC"

Switched compiler from gcc/g++ to MSVC, that's only solution :(

What's #pragma comment (lib, lib/glut32.lib )?

This pragma allows the library author to define library imports based on a range of criteria that can be analysed at compile-time. For instance, you can link against different libs, based on whether you want to link with:

  • multithreading on or off;
  • the shared or static version of the library in question;
  • the shared or static version of the standard runtime library;
  • the debug or release library;
  • the 32-bit or 64-bit library.

With the pragma form, all this can be decided via #ifdef preprocessor tests in one of the library's header files.

How does #pragma align work?

Although it begins with #, #pragma is not a preprocessor directive, instead it is handled by the compiler.

Pragma directives are compiler-specific, so the specifics of how they work depend on the compiler.

It is not standard: C++11 uses the alignas specifier to achieve this. Older compilers have alternatives (such as MSVC _declspec(align(4))), and continue to support these for compatibility with existing source code.

That said, where supported #pragma align is reasonably similar between compilers, and works in exactly the way you describe, individually specifying the alignment of data types and members of structures. It certainly exists for all common x86 compilers.

As to how it is implemented, that is compiler specific. But in effect the compiler must tag the internal metadata for the type with its alignment requirement so that the correct machine code can be generated, and offsets to struct members calculated correctly, sizeof and pointer arithmetic works, and so forth. Each data type has a size and an alignment requirement anyway, and each member has an offset, so for a pragma to change them just involves changing what information the front-end sends to the back-end.

what does this code mean?

#pragma is a directive to the compiler. In this case, it asks the compiler to put a "comment" into the final object file, and this comment is then used by the linker to link against the library.

Then it initializes the SDL library.

Then it registers SDL_Quit function to be executed at program exit.

Then pause, otherwise the program quits immediately.



Related Topics



Leave a reply



Submit