48

My background is C# but I have to maintain some legacy (MS) C++. In that codebase I stumpled 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 dependant 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 dependant 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'?

pnuts
  • 58,317
  • 11
  • 87
  • 139
steglig
  • 1,098
  • 1
  • 9
  • 15

3 Answers3

80

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 you information about what symbols are exported in the dll. So basically you only need the lib to compile & link, but you need the dll to run the program, as it contains all the binary code.

You say there's an associated dll, which usually indicates the lib file only contains linking info, and no code. You should get a run-time error if the associated dll was not found. You can check with MSVS if a different version of the dll was loaded or if it was loaded from a different place.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • *"You only need the lib to compile & link"* .. so the lib can be like an alternative to a header file (.h)? – Ari Seyhun Sep 20 '17 at 03:14
  • @Acidic I think what Luchian means is that the `dll` won't be necessary in that case. You'll find that the header file still is. – Marc.2377 Oct 29 '19 at 08:56
  • Where can we put it though? Is it fine if we add it in main.cpp or can we include it anywhere (only once)? – KeyC0de Oct 25 '20 at 09:04
13

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.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • Yes 700 normally will be some versioning however windows will look for an exact match and not try to load higher versioned library with the same name. – drescherjm Aug 30 '12 at 15:00
  • How will I know whether the other lib is an import or a static library? So if it's static its code is included in 'my' program? – steglig Aug 30 '12 at 15:10
  • 1
    I believe the answer in the following describes how to tell the difference: http://stackoverflow.com/questions/6402586/know-if-lib-is-static-or-import – drescherjm Aug 30 '12 at 15:26
  • 4
    Wrong answer. The linker never looks for DLLs, it looks for .lib. The info inside the .lib tells the linker what to do next - to emit the code from it directly (aka static linking) or to emit imports to a .dll (the name of the .dll is also encoded inside the .lib). – rustyx Aug 23 '16 at 13:26
  • 1
    @RustyX Correct, and that is what I meant when I wrote that. I will clarify soon. – drescherjm Aug 23 '16 at 14:22
-1

If the .lib is a "real" lib with the actual code (I've never used DLLs save for the system-provided ones, but I believe you make 'import libs' for your own DLLs), then the DLL isn't required.

As for the subject, #pragma comment(lib,xxx) allows programs to add certain options for the linker. Can be very useful, although I've missed a few options that I would have liked to add like this. The example given is a prime example for its use: when the object file is included in the program, then the lib specified will be added as well.

Christian Stieber
  • 9,954
  • 24
  • 23