5

I am working on this Windows program from 1999. I was unable to build it in Visual Studio as it kept giving me linker errors about missing symbols. Then my collegue told me to try taking .lib files from original project and try to specify them as additional link dependencies. I did it and the program linked fine. When I tried to run it, it complained about missing dll files. So I found an existing copy of the Windows program running on some old computer, copied the dll files and my build started working! It was the happiest day in my life but I don't quite know what happened.

Can anyone briefly explain what are lib files in Windows and how they relate to dlls?

the_mandrill
  • 29,792
  • 6
  • 64
  • 93
bodacydo
  • 75,521
  • 93
  • 229
  • 319
  • possible duplicate of [Visual Studio: What exactly are lib files (used for)?](http://stackoverflow.com/questions/2375065/visual-studio-what-exactly-are-lib-files-used-for) – In silico Mar 19 '12 at 21:50
  • 1
    I've edited the title to make the question more explicit -- it's not a dupe of the above. – the_mandrill Mar 20 '12 at 08:52

2 Answers2

8

There are two uses of .LIB files on Windows. Ken mentions one of them, which is for static linking, but there is another use, which is called an import library, which is what you have here. If you build a .DLL yourself, you have the option of generating an import library for it. The effect of this is that you can just link against the .LIB file, as you would for a regular static library, but the .LIB actually just contains the boilerplate code to load the entry points from the DLL.

The reason this is useful is that you may need to distribute code as a dll (eg so you can update it independently of the main application, or supply a dll as a plugin), but it makes linking to the application easier because the .LIB deals with the LoadLibrary() and GetProcAddress() calls so that you don't have to. It can also load class definitions from a dll, which you can't do with GetProcAddress() yourself.

More info on MSDN

the_mandrill
  • 29,792
  • 6
  • 64
  • 93
  • Good catch. It's been so long since I worked with plain old C/C++ that I forgot this one (and it's the right one). You should edit your answer to include the other use (the one I mentioned), and I'll delete my answer. :) – Ken White Mar 20 '12 at 01:39
  • I am trying to link to `freeglut_static.lib` and I still get missing dll, `freeglut.dll`. What exactly should I do in visual studio to link to non-import lib file? I have set `/MT` option and additional dependency to `freeglut_static.lib` instead of `freeglut.lib`. – Necktwi Jan 01 '17 at 13:06
0

.LIB (library) files are collections of compiled source files (object files) that you can link into your application to provide functionality. Linking makes the object files used part of your executable itself. This is typically known as static linking, and it's why you got the compile time error about missing symbols; the library files weren't available to extract the needed object code from in order to add it to your application.

.DLL (dynamic link library) files are compiled source files that you can load at runtime and use specific functionality (usually by name) from; they're not part of your executable, but are loaded at runtime from the DLL itself. (Not having the DLLs available, as you've already experienced, means your app doesn't run.) They're not needed at compile time, but only at run time.

Some IDEs (such as Visual Studio C/C++) make both a static .LIB file and a .DLL version available, so you can choose to have the run time libraries linked directly into your application or have them available to load dynamically at run time. (The second is useful, for instance, if you have several applications you've developed; you can use the dynamic version of the MSVCRTL to drastically reduce the size of your executables by distributing the RTL seperately instead of being linked into each application.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Funny thing is that I needed both `.LIB` and `.DLL` to run my application! I wasn't able to compile without `.LIB` and I wasn't able to run it without `.DLL`. – bodacydo Mar 19 '12 at 22:13
  • They were probably unrelated. Hard to tell, though, since you provided no info about **which** libraries and DLLs. :) – Ken White Mar 20 '12 at 01:15