C++ Visual Studio: linking using pragma comment

user1612986 picture user1612986 · Oct 10, 2012 · Viewed 35.9k times · Source

I came across a piece of code which uses #pragma comment(lib, "libraryname").

Why this type of usage as opposed to just linking the library from the properties menu? In what situations is this usage called for? I am in windows using C++ Visual Studio 2010.

It would be nice to see an example which calls for this type of usage.

Answer

Alex F picture Alex F · Oct 10, 2012

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.