What does #pragma comment mean in the following?
#pragma comment(lib, "kernel32")
#pragma comment(lib, "user32")
#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 microsoft.com
I've always called them "compiler directives." They direct the compiler to do things, branching, including libs like shown above, disabling specific errors etc., during the compilation phase.
Compiler companies usually create their own extensions to facilitate their features. For example, (I believe) Microsoft started the "#pragma once" deal and it was only in MS products, now I'm not so sure.
Pragma Directives It includes "#pragma comment" in the table you'll see.
HTH
I suspect GCC, for example, has their own set of #pragma's.
The answers and the documentation provided by MSDN is the best, but I would like to add one typical case that I use a lot which requires the use of #pragma comment to send a command to the linker at link time for example
#pragma comment(linker,"/ENTRY:Entry")
tell the linker to change the entry point form WinMain() to Entry() after that the CRTStartup going to transfer controll to Entry()
These link in the libraries selected in MSVC++.
Pragma directives specify operating system or machine specific (x86 or x64 etc) compiler options. There are several options available. Details can be found in https://msdn.microsoft.com/en-us/library/d9x1s805.aspx
#pragma comment( comment-type [,"commentstring"] ) has this format.
Refer https://msdn.microsoft.com/en-us/library/7f0aews7.aspx for details about different comment-type.
#pragma comment(lib, "kernel32")
#pragma comment(lib, "user32")
The above lines of code includes the library names (or path) that need to be searched by the linker. These details are included as part of the library-search record in the object
file.
So, in this case kernel.lib and user32.lib are searched by the linker and included in the final executable.
Related
I'm providing a Microsoft C++ library (static and dynamic) for other clients in my company. My library has a few classes that depend upon functions located in common Windows DLLs whose import libraries aren't included in the project link settings by default.
Rather than add a new import library name to the linker section of every build configuration of my library each time a new dependency like this came up, I thought it would be better in terms of encapsulation of dependencies (and a bit more self-documenting) if I used #pragma comment(lib, ...) in the .cpp file of the class that depended upon a function from such a DLL, e.g.:
Foo.cpp:
#include <SomeWinLib.h>
#pragma comment(lib, "somewinlib.lib")
... implementation of Foo class ...
...but I found that this was causing link warnings/errors for clients of my library if they were also using SomeWinLib. After doing some reading on the subject, it seems that the recommended/best practice is to let the client who is linking in my library also link in the libraries it depends upon.
I still wanted this to be as automatic/painless as possible for the clients, so rather than just put a list of required import libraries in a readme.txt file (and then sit back and wait for the inevitable phone calls from frustrated developers), I'd just place the #pragma comment in the Foo.h header file. That way, clients would automatically link in the required library if they included the header file of a class that required it.
Foo.h:
#pragma comment(lib, "somewinlib.lib")
public class Foo
{
However, when I build my library, Foo.cpp includes Foo.h obviously, so it would seem that I need some kind of preprocessor "guard" around the #pragma comment line in the header file so that it's only seen by the preprocessor when clients include my header file.
Foo.h:
#if !defined(building_my_library)
#pragma comment(lib, "somewinlib.lib")
#endif
public class Foo
{
All of this seems like something that pretty much EVERY library must be doing, yet I don't see examples of this when I Google other open source Windows libraries. That makes me suspect I've missed something somewhere.
Does anyone know if I'm understanding the issues correctly, and if so, whether I'm over-complicating the solution?
Thanks!
Your last option is more or less what the boost headers do, except they also provide a preprocessor directive to explicitly turn off the auto-linking in case you want to control things yourself:
#if !defined(building_my_library) && !defined(no_auto_link_stuff)
#pragma comment(lib, "somelib.lib")
#endif
A solution that you might not have considered is to use runtime linkage to the DLL via LoadLibrary and GetProcAddress, removing the dependence on somewinlib.lib.
I'm using VS2010 to write some win32 app. I normally add .lib files to linkage using to project property manager. yet, sometimes, when I just want to test an API function , I don't want to modify my project file , but rather just add a removable line for linking with the required lib
What is the linker keyword for linking with a given .lib file?
You can use #pragma comment command
Example:
#pragma comment (lib, "d3d9.lib")
Another example:
#pragma comment (lib, "yourlibrary.lib")
Just put in hmm.. before int main(){}
http://support.microsoft.com/kb/153901
http://msdn.microsoft.com/en-us/library/7f0aews7%28v=vs.71%29.aspx
If there are problems:
Ignore #pragma comment(lib, ...)?
Unresolved external symbol when linking my library
When I am using a non-standard library in C++ I have to add the library name in linker-->input-->additional dependencies . But the alternative is to write
#pragma comment(lib , "library name")
Are these two methods completely the same?
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.
These are two ways of doing the same thing. i.e. the compiler telling the linker which libs to look for. #pragma comment has other functions too like adding data like compiler info or any other meta data to the object file.
#pragma comment is VC++ specific.
If I wanted to add, let's say, a new .lib to the build only if a particular #define was set, how would I do that?
In the MSVC++ 2008 "Property Pages", you would simply add: Config Properties -> Linker -> Input -> Additional Dependencies, but I would like it if something like #define COMPILE_WITH_DETOURS was set, then the particular library would be added to the dependencies, otherwise it would be removed.
You can set some linker options by using #pragma comment in one of your source files.
For example, to link against a 'detours.lib' library only if COMPILE_WITH_DETOURS is defined, you can use:
#ifdef COMPILE_WITH_DETOURS
# pragma comment(lib, "detours.lib")
#endif
(this is specific to Microsoft Visual C++ and is not portable)
I found a source code on MSDN about how to enable/disable privileges in C++
According to the source code, the linker must include cmcfg32.lib, but it can't be found...
I tried to compile without including that lib, it compiles without any error, but when I launch my program, it crashes with a fatal error.
So please, if you know which SDK contains cmcfg32.lib, let me know ;)
Thanks!
It looks (to me) like a minor error in the code. Delete the line:
#pragma comment(lib, "cmcfg32.lib")
Of, if you want the correct library linked automatically, change it to:
#pragma comment(lib, "advapi32.lib")
This code links for me without trouble, using the 6.0a version of the SDK. "cmcfg" googles as Connection Manager Configuration, no idea what that is or why it would be needed here.
Just delete the #pragma.