C++ 如何静态链接 .DLL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/424032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:16:40  来源:igfitidea点击:

How to link a .DLL statically?

c++dlllinkerstatic-linking

提问by mmmmmmmm

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#.

我们有一个由 VS 构建的(纯原生 C++).DLL。作为客户端,我们有一些本地 C++ 应用程序和一个围绕这个 DLL 的 .Net-Wrapper,用 C++/CLI 编写。最后,还有一些用 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. 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!

我的问题是 native.dll 必须以与 .Net 世界不同的方式分发,并且 VS 不跟踪该 DLL。因此,为了让我所有的 C# 应用程序正常工作,我必须将它复制到每个可执行目录或将其放在 %PATH% 中的某个位置(我会在开发人员计算机上避免这样做,因为他们可能希望使用不同版本的 DLL 启动不同的应用程序)。如果存在引用 Wrapper-DLL 的 UserControl,则会出现更大的问题:您必须将 DLL 复制到 VS 的目录或再次复制到 %PATH%。但最坏的情况发生在我们的翻译工具上。该工具跟踪 .Net-Assemblies 并将它们打包到可以发送给外部翻译器的翻译器包中。据我所知,无法将本机 .DLL 放入该包中!

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.

所以我计划将本机 DLL 静态链接到 .Net-Wrapper,这将解决我的问题。但是对于我们的本机应用程序,这个本机 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
  • Find a way to let VS generate two outputs from one project
  • 制作两个项目(一个生成静态库;一个创建动态库 => 我尽量避免这种情况)
  • 找到静态链接 DLL 的解决方案
  • 找到一种方法让 VS 从一个项目中生成两个输出

回答by BmanInHouston

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).

在 dll 的 C++ 项目文件中,创建两个配置,一个生成 DLL,一个生成 .lib。两个项目不是必需的,因为任何 .NET/C++ 项目都可以支持多个构建配置(这是 Release 和 Debug 版本构建的不同方式)。

回答by Ismael

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.

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

回答by SmacL

Pick up a copy of DLL to Lib(Edit: If you can't find a cheaper option)

拿起一份DLL 到 Lib(编辑:如果你找不到更便宜的选择)

回答by kiriloff

You can generate a dll and export the entry point to a lib using dllexport, this is explained here

您可以生成一个 dll 并使用 将入口点导出到一个库dllexport,这在此处进行了说明

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

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