11

Consider this situation:

I'm creating a DLL (let's call this dllA), which, in turn, calls functions in other DLLs (let's call these dllX, dllY and dllZ). If someone else wanted to use dllA, they'd need a copy of dllX, Y and Z as well.

Is there a way to build dllA such that the needed functions in dllX, Y and Z get linked into dllA? I don't know if this is the right terminology, but is this called "statically linking a DLL"?

I'm looking for a simple solution. I'd be happy with something crude that simply concatenates dllX, Y and Z and appends that to dllA somehow, if such a thing exists. But I have this bad feeling that there's no easy solution to this.

I'm on Windows, using VS.

Thanks!

1
  • Do you have the source code of all the dlls? Commented Sep 29, 2009 at 5:36

3 Answers 3

11

You can't statically link a DLL, only a LIB. DLLs are self contained and have static members and DllMain load/unload dependencies that means they cannot be split into containing functions and linked into a different executable.

You should create an installer that deploys all the appropriate dlls, as needed.

If you really think is that important and you cannot rely on your app installer you can create a wrapper that embeds the needed dlls (A, X, Y and Z) as resources and at runtime extracts them and write them to disk in the user profile temp folders, then loads the extracted dllA and defers all incoming function calls to the loaded dllA. Some of the sysinternals tools used similar techniques to extract x64 images of the application from the x86 image and relaunch themselves as 64bit processes w/o having to ship two separate exes.

1
  • I love this idea of extracting the DLL into the temp folder... Any example/tutorial on how to do it?
    – Sam
    Commented Sep 26, 2020 at 22:56
8

Technically, you can not statically link a DLL, but 'Implicit linking' is equivalent to it.

  1. Include header file declarations using __declspec(dllimport) specifier in the executable.
  2. A .lib of the same name as the dll is generated along with the DLL when it is built, called its 'Import Library'. Link this lib to executable.
  3. Place dll alongside exe.

Then windows will load the DLL whenever the exe is called, effectively acting as a static lib.

2

I don't believe you can. I haven't heard of it being done. If you have build access to your other DLLs then you can build them as static libraries and that will work as you want but there's a reason why they're called dynamic link libraries.

EDIT: Just found this thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.