How does dynamic linking work generally?
On Windows (LoadLibrary), you need a .dll to call at runtime, but at link time, you need to provide a corresponding .lib fi
In shared library, such as .dll
.dylib
and .so
, there is some information about symbol's name and address, like this:
------------------------------------
| symbol's name | symbol's address |
|----------------------------------|
| Foo | 0x12341234 |
| Bar | 0xabcdabcd |
------------------------------------
And the load function, such as LoadLibrary
and dlopen
, loads shared library and make it available to use.
GetProcAddress
and dlsym
find you symbol's address. For example:
HMODULE shared_lib = LoadLibrary("asdf.dll");
void *symbol = GetProcAddress("Foo");
// symbol is 0x12341234
In windows, there is .lib
file to use .dll
. When you link to this .lib
file, you don't need to call LoadLibrary
and GetProcAddress
, and just use shared library's function as if they're "normal" functions. How can it work?
In fact, the .lib
contains an import information. It's like that:
void *Foo; // please put the address of Foo there
void *Bar; // please put the address of Bar there
When the operating system loads your program (strictly speaking, your module), operating system performs LoadLibrary
and GetProcAddress
automatically.
And if you write code such as Foo();
, compiler convert it into (*Foo)();
automatically. So you can use them as if they're "normal" functions.