Re: how do static import libraries actually work?



Hi Tim Roberts,

Thank you.

"I could create an import library called xxx.lib that contained an entry
for
MyEntryPoint that points to YourEntryPoint in zzz.dll. " - How?

--
Thanks & Regards,
John.


"Tim Roberts" wrote:

kilik3000@xxxxxxxxx wrote:

One thing I've never really understood are import libraries for DLLs
on Windows.

For example, let's say that I use a function from user32.dll in my
code. Without linking to user32.lib in VS I get a link error, so I
link to user32.lib and all is well.

I guess what I don't understand is how this jives with static linking
and dynamic linking.

From what I understand, static linking is when you take object code
and just plop it into your binary.

Dynamic linking (on windows at least) is when you call LoadLibray()
and then GetProcAddress() and then make your call on a function
pointer.

What happens when you statically link to a DLL import library (i.e.
user32.lib)?

Does the import library object code contain implicit calls to
LoadLibray() and GetProcAddress that are performed on your behalf?

It is kind of an interesting process. The object file produced by the
compiler just calls, for example, CreateWindowA. The object file doesn't
know where that entry point will come from, it relies on the linker to fix
it.

In this particular case, the CreateWindowA entry point will be found in
user32.lib. But the library entry for CreateWindowA doesn't actually
contain any code. Instead, it contains a special linker table that says
"this entry point is found in CreateWindowA in user32.dll". The linker
creates an entry in the import descriptor table in the resulting
executable.

Although it is most common for the library entry point name to match the
DLL entry point name, and it is also most common for all of the imports in
a single library to point to a single DLL, neither is required. I could
create an import library called xxx.lib that contained an entry for
MyEntryPoint that points to YourEntryPoint in zzz.dll.

When the executable is loaded, the OS loader runs through the import
descriptor table, loads all of the DLLs that are mentioned, and fixes up
the addresses.
--
Tim Roberts, timr@xxxxxxxxx
Providenza & Boekelheide, Inc.

.



Relevant Pages

  • Re: how do static import libraries actually work?
    ... One thing I've never really understood are import libraries for DLLs ... What happens when you statically link to a DLL import library (i.e. ... compiler just calls, for example, CreateWindowA. ... know where that entry point will come from, it relies on the linker to fix ...
    (microsoft.public.vc.language)
  • Re: compile+link Fujitsu Linux
    ... Red Hat Linux 7.2, Locale C ... There is no Cobol syntax to specify a library name. ... It searches only the libraries named in the executable's ELF ... Libraries normally contain many programs and entry points. ...
    (comp.lang.cobol)
  • Re: libstdc++ version bump.
    ... change (20041001 entry). ... Are you sure that the problem is with libstdc++ and not the above libraries? ... > How can we bump it? ...
    (freebsd-stable)
  • Re: NVidia glx stuff dies in sysarch(I386_SET_LDT, ...)
    ... >> We can simply fail if the process is trying to set a LDT entry that's ... > how it will be affected by the use of threading libraries ... > (or other libraries using threads). ...
    (freebsd-current)
  • RE: Dynamic Linking Increases Program Size
    ... Wouldn't we get duplicate symbol errors if linking ... with both the static and DLL libraries? ... Note that the largest of the two libraries exports ... Most of these are annoying little functors. ...
    (microsoft.public.windowsce.embedded.vc)