CiaranWelsh
CiaranWelsh

Reputation: 7681

Misconception about static/implicit linking Vs dynamic/explcit linking

I've recently learnt that static linking and implicit linking are basically the same thing, just different nomenclature. My understanding is that when you statically (implicitely) link to a binary, you are by definition linking against a *.lib (windows) or *.a (linux) file, often using target_link_libraries in cmake. On the other hand when you explicitely link (using LoadLibrary on windows) you are by definition linking to a *.dll file (or *.so on linux) (and there is no corresponding cmake command because all the work is done inside the actual code).

However, in multiple places I've read people referring to statically/implicitely linking to a dll file, which has confused me. Clearly there is a hole in my knowledge somewhere and I was hoping somebody here could plug it.

Edit

Its been pointed out that this question refers mainly to windows, which it does. However, I am currently trying to produce cross platform code so I am still interested on how (or if) these concepts generalise to other platforms.

Upvotes: 1

Views: 500

Answers (1)

Employed Russian
Employed Russian

Reputation: 213897

There are actually 3 different kinds of linking, not 2.

For UNIX:

  1. Link against archive (aka static) library:

    gcc main.o libfoo.a

  2. link against dynamic (aka shared) library:

    gcc main.o libfoo.so

  3. Link against libdl, which allows you to dlopen arbitrary other shared libraries (which don't need to exist at the time of the link):

    gcc main.o -ldl

Both 2 and 3 involve dynamic linker (and are using shared libraries), but to a different extent.

An equivalent exists on Windows: when you link against foo.lib, you are using either 1 or 2, depending on whether foo.lib contains actual code, or refers to foo.dll.

When you use LoadLibrary, you are in case 3.

Upvotes: 3

Related Questions