26

There are things that I don't understand when it comes to linking... I'm writing a program using a 3rd party library (the GEOS library). This program has a dependency to geos.lib but still needs geos.dll to run.

I read this question, I think I understand the difference between static and dynamic libraries. What I don't understand is why I still need a dll when I statically link a library.

Community
  • 1
  • 1
undu
  • 2,411
  • 3
  • 21
  • 28

5 Answers5

35

There are 3 kinds of libraries on Windows:

  • object library (*.lib)
  • import library (*.lib)
  • dynamic library (*.dll)

object libraries are statically linked. They contain the full object definitions of the code abstracted by the library.

import libraries is a special form of an object library. Instead of containing code they contain information for the linker that ultimately maps the executable file to the dynamic-link library.

dynamic link libraries, like object libraries, supply code for your program. However, this code is loaded at runtime and not compiled into your exe.

You don't always need to link an import library. Instead you can call LoadLibrary() and lookup the API entry points by name or ordinal. (You always have to tell the code which DLL and where in that DLL's API you want to enter.)

The other comments here are correct in that you cannot make a DLL into a static lib without recompiling the code for the libary -- it is a different kind of output.

brad-tot
  • 783
  • 7
  • 9
  • 5
    [Beginner's Guide to Linkers](http://www.lurklurk.org/linkers/linkers.html) is a great article about linking. I found it this week while researching this exact same question. – Pressacco May 22 '15 at 14:39
25

It's not statically linked. The .lib is just a stub library that binds in the .dll on windows. That is, you link with the .lib at compile time, and then at runtime it will go looking for the .dll.

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • Aren't there any options which allow to statically link code from particular .dll file? – Bartek Banachewicz Jun 15 '12 at 13:26
  • 1
    I think you have to compile the library for static linking, I'm not sure you can change a dll into a statically linked library after it has been compiled. – Andrew Tomazos Jun 15 '12 at 13:27
  • @Andrew So dll files always come with a .lib ? How do I know when the .lib isn't sufficient and I need to provide dll ? – undu Jun 15 '12 at 13:33
  • 1
    @undu: On Windows, yes you always need to link with a .lib to dynamically link with a dll. The dll supplier will always include this for you somewhere. You mean how do you know if the .lib is a static library? You can inspect these properties in visual studio I think. – Andrew Tomazos Jun 15 '12 at 13:50
  • @undu If the lib you link with is a static linked lib exporting functions from a dll, then, if the dll is not available in the search path, your application will fail to launch. You can have [load-time dynamic linking](http://msdn.microsoft.com/en-us/library/windows/desktop/ms684184(v=vs.85).aspx) or [run-time dynamic linking](http://msdn.microsoft.com/en-us/library/windows/desktop/ms685090(v=vs.85).aspx). Also there's a linker flag (don't remember the name) that allows you to postpone load-time dynamic linking to the moment the first function from the dll is called. – Alexandru C. Jun 15 '12 at 14:06
  • I hate to resurrect an old post, but this was my first google result. https://msdn.microsoft.com/en-us/library/yx9zd12s.aspx /DELAYLOAD defers DLL loading until first call. – Soylent Graham Jun 30 '16 at 16:52
  • Related question on how to know if the lib is an object library or import library: https://stackoverflow.com/q/6402586/11722 – Zitrax Jan 26 '18 at 13:53
2

If .lib was created by Visual Studio then check value of Project properties -> Linker -> Input -> Module Definition File. If it's not empty then link.exe create stub library instead of static library even if Project properties -> General -> Configuration Type is "Static library (.lib)".

0

You are definetely linking to a dynamic library. Just because the linker requires .lib file doesn't mean you're linking to a static library.

Mohammad
  • 1,253
  • 1
  • 10
  • 26
0

You can statically link the lib file if and only if this is a static lib file. So first you need to convert your dll's project to the static lib, build it and after that use the product of your build which will be a static .lib file.

AlexTheo
  • 4,004
  • 1
  • 21
  • 35