Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link a .DLL statically?

We have a (pure native C++) .DLL that is build by VS. As clients we have some native C++ applications and a .Net-Wrapper around this DLL written in C++/CLI. Finally there are some client applications for the .Net-Wrapper written in C#.

My problem is that the native.dll must be distributed in a different way than the .Net world works and the VS does not keeps track of that DLL. So to let all my C# Apps work correctly I have to copy it to each executable directory or put it somwhere in %PATH% (which I would avoid on developer computers since they may want to start different apps with different versions of the DLL). Even bigger problems occur if there are UserControls that reference the Wrapper-DLL: You have to copy the DLL to VS's directory or again to %PATH%. But the worst case occurs with our Translator tool. This tool keeps track of .Net-Assemblies and packs them into Translator-packages that can be send to an external translator. As far as I know there is no way to put the native .DLL into that package!

So I plan to link the native DLL statically into the .Net-Wrapper which would solve my problems. But for our Native applications this native DLL must still be a DLL.

So I have two options:

  • Make two projects of that (one that generates a static library; and one that creates a dynamic one => I try to avoid this)
  • Find a solution to link DLLs statically
  • Find a way to let VS generate two outputs from one project
like image 513
mmmmmmmm Avatar asked Jan 08 '09 12:01

mmmmmmmm


People also ask

Can a DLL be statically linked?

The DLL can link to the same MFC static link libraries used by applications. There is no longer a separate version of the static link libraries for DLLs.

Can a DLL link to static library?

When your DLL refers to an external content (like function or variable), it is resolved at linking time - together with all dependencies. But that's all. If your static library has a function named print_sample_string() , but your DLL does not use it, it won't be attached to DLL image.

How do I link a DLL to a DLL?

You can use load-time dynamic linking or run-time dynamic linking in your DLL in the same way as in the executable. The only restriction is not to call LoadLibrary from your DllMain function to avoid deadlocks.


2 Answers

In the C++ project file for the dll, create two configurations, one that generates a DLL and one that generates a .lib. Two projects are not necessary, since any .NET/C++ project can support multiple build configurations (this is how Release and Debug versions build differently).

like image 142
BmanInHouston Avatar answered Oct 02 '22 13:10

BmanInHouston


Another option is to have two projects, one project will output a .lib which can be statically linked, and a second project which will output a .dll and will have your .lib as dependency, you should add .def to your .dll with the symbols that you are planning to export, or else it will be empty.

like image 40
Ismael Avatar answered Oct 02 '22 13:10

Ismael