Re: DLL and external references

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: William DePalo [MVP VC++] (willd.no.spam_at_mvps.org)
Date: 07/15/04


Date: Thu, 15 Jul 2004 14:46:59 -0400


"alias.name" <alias.name@gmail.com> wrote in message
news:Xns9527735735581aliasnamegmailcom@207.217.125.205...
> This is probably a basic question:

It is.

> When linking I do NOT specify the static library in which
> "external_function" exists in.
>
> This causes a linker error "extern int external_function()" unresolved
> external.
>
> The question is, do all external references in a DLL have to be resolved
at
> link time?

Short answer: Yes because you are using an "implicit" linking mechanism

Longer answer: Yes, but you can do better if necessary.

The issue is that your code makes an explicit reference to a function
external to the compilation unit. The compiler marks it as such. In the
world before DLLs, it was the job of the linker to "resolve" external
references and "bind" them to addresses by adding object code from external
sources (libraries and other compiled objects) to the executable image.

This behavior of the compiler doesn't change when DLLs are present. Adding
the the DLL's import library resolves all the references to each imported
function to a single address which contains a jump instruction whose target
is left blank. This unresolved reference is called an "import". At load
time, Windows maps the DLLs that you reference into your process, notes the
addresses of all of the (possibly relocated) imported functions contained in
the DLLs and then patches the import table so those jump instructions are
not jumps to nowhere.

With that simplified explanation, you can see if you don't supply an import
library for the DLL that you will have unresolved references. That's a
no-no.

There are at least two ways to defer the resolution of imports until
runtime. One is to call the DLL functions via pointers. You can get the
pointer to a function by calling LoadLibrary() followed by GetProcAddress().
The other way is to let the compiler and linker do this for you, it is
called delay loading.

Both techniques, have their place. But for now, I think you should go with t
he simple answer. :-)

Regards,
Will



Relevant Pages

  • Dependencies vs. Merge Modules
    ... Whenever I create a Setup Project for an application I'm working on, ... Visual Studio automatically detects that application's dependencies. ... the application project's References folder. ... a collection of DLLs) are distributed. ...
    (microsoft.public.vsnet.general)
  • Interop errors
    ... I have an ASP.NET website that has several references to COM dlls. ... My website won't compile and I'm seeing several errors currently, ... Dim myScriptingVar1 as new Scripting.Dictionary ...
    (microsoft.public.dotnet.framework.interop)
  • Re: How to test if a DLL (library) exists
    ... will see the referenced dlls in the dialog that comes up. ... dlls as librarys through vba. ... I have done it with regsrv for windows files before but only through ... > How do I test in VBA to see if a particual DLL exists in my references? ...
    (microsoft.public.excel.programming)
  • Re: Strong Names and Referenced assemblies
    ... I built another VB.NET app that has no references whatsoever to any COM dlls. ... installed this VB.NET exe on my local machine and it runs perfectly on my ...
    (microsoft.public.dotnet.security)
  • Re: HowTo: Unregister a DLL/Control when the File no longer Exists
    ... There is the main EXE and a few ActiveX DLLs. ... The main project references one of the DLLs, ... DLLs both register and unregister themselves through function ... There should also be an API call for it. ...
    (microsoft.public.vb.general.discussion)