简体   繁体   English

如何静态链接.DLL?

[英]How to link a .DLL statically?

We have a (pure native C++) .DLL that is build by VS. 我们有一个由VS构建的(纯本机C ++).DLL。 As clients we have some native C++ applications and a .Net-Wrapper around this DLL written in C++/CLI. 作为客户端,我们有一些本机C ++应用程序和围绕这个用C ++ / CLI编写的DLL的.Net-Wrapper。 Finally there are some client applications for the .Net-Wrapper written in C#. 最后,有一些用C#编写的.Net-Wrapper客户端应用程序。

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. 我的问题是native.dll必须以与.Net世界不同的方式分发,并且VS不会跟踪该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). 因此,要让我的所有C#应用程序正常工作,我必须将其复制到每个可执行文件目录中,或者将其放在%PATH%中(我会避免在开发人员计算机上,因为他们可能希望使用不同版本的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%. 如果存在引用Wrapper-DLL的UserControl,则会出现更大的问题:您必须将DLL复制到VS的目录或再次复制到%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. 该工具跟踪.Net-Assemblies并将它们打包到可以发送给外部翻译器的Translator-packages中。 As far as I know there is no way to put the native .DLL into that package! 据我所知,没有办法将原生.DLL放入该包中!

So I plan to link the native DLL statically into the .Net-Wrapper which would solve my problems. 所以我打算将原生DLL静态链接到.Net-Wrapper,这将解决我的问题。 But for our Native applications this native DLL must still be a DLL. 但对于我们的Native应用程序,此本机DLL仍必须是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 找到一个静态链接DLL的解决方案
  • Find a way to let VS generate two outputs from one project 找到一种让VS从一个项目生成两个输出的方法

In the C++ project file for the dll, create two configurations, one that generates a DLL and one that generates a .lib. 在dll的C ++项目文件中,创建两个配置,一个生成DLL,另一个生成.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). 两个项目不是必需的,因为任何.NET / C ++项目都可以支持多个构建配置(这就是Release和Debug版本以不同方式构建的方式)。

另一种选择是有两个项目,一个项目将输出一个可以静态链接的.lib,另一个项目将输出.dll并将你的.lib作为依赖项,你应该将.def添加到你的.dll中您计划导出的符号,否则它将为空。

拿起DLL的副本到Lib (编辑:如果你找不到更便宜的选项)

You can generate a dll and export the entry point to a lib using dllexport , this is explained here 您可以使用dllexport生成一个dll并将入口点导出到lib,这在此处进行了解释

http://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx http://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM