Search code examples
c++pragma

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


I have to maintain some legacy (MS) C++. In that codebase, I stumbled across:

#pragma comment(lib, "OtherLib700.lib")

Where 700 is some versioning. Besides, the lib is a DLL with the same name.

I first thought that the program would be dependent upon the DLL, but after removing it from the system, the program still works. There exists a newer version of the DLL, though, which is named OtherLib900.

It seems as though the program 'included' the code of the lib so that it's no longer dependent upon the external DLL. (Or that the program 'automatically' uses the newer DLL.)

Which one is correct? Is there are way to further confirm that 'assumption'?


Solution

  • 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.

    The documentation for #pragma comment( lib libname) states the following:

    Places a library-search record in the object file. This comment type must be accompanied by a comment-string parameter that has the name (and possibly the path) of the library that you want the linker to search. The library name follows the default library-search records in the object file. The linker searches for this library the same way as if you specified it on the command line, as long as the library isn't specified by using /nodefaultlib. You can place multiple library-search records in the same source file. Each record appears in the object file in the same order it's found in the source file.