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.
LoadLibrary
you should not need to link to any specific library. That's the point of dynamically loading libraries. It's the same on other platforms. – Terceiradlopen
withoutdlsyms
following that call, you'll get nothing. So Windows and *nix are similar when it's about dynamic DLL loading. The are different when it's about linking to the dll at link-time. – Markley