在C ++项目中的位置以及如何包含dll文件? [英] Where to place and how to include dll files in c++ project?

查看:213
本文介绍了在C ++项目中的位置以及如何包含dll文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了此指南引导您完成使用Cairo和Visual C ++创建可视应用程序的必要步骤。该指南建议您下载某些dll文件,并将它们存储在创建可执行文件的目录中(调试)。
这是Nil在他的教程中引用的文件列表:

I read this guide that walks you through the steps necessary to create a "visual" application with Cairo and Visual C++. The guide suggests you download certain dll files and store them in the directory in which your executable is created (debug). Here is the list of the files Nil is referring to in his tutorial:


  • cairo Binaries(是的,您需要Binaries软件包同样,因为开发人员也不包含DLL)-> libcairo-2.dll

  • zlib Binaries-> zlib1.dll

  • libpng二进制文件-> libpng12-0.dll

  • Freetype二进制文件-> freetype6.dll

  • FontConfig二进制文件-> libfontconfig-1.dll

  • expat Binaries-> libexpat-1.dll

  • cairo Binaries (yes you need the binaries package too, as the Dev one doesn't contain the DLL) -> libcairo-2.dll
  • zlib Binaries -> zlib1.dll
  • libpng Binaries -> libpng12-0.dll
  • Freetype Binaries -> freetype6.dll
  • FontConfig Binaries -> libfontconfig-1.dll
  • expat Binaries -> libexpat-1.dll

如您所见,其中有很多文件。我一直在想这是否是正确的方式?在这种情况下,是否有另一种方法被认为是最佳实践?

As you can see, it's quite a lot of files. I've been wondering if this the "correct" way of doing this? Is there an alternative way that is considered "best-practice" in this case?

推荐答案

这种方法没有什么问题,并且这可能是启动和运行应用程序的最快方法。您的应用程序需要这些库,因此将它们放在同一文件夹中就可以找到它们。

There's nothing wrong with this approach, and it's probably the quickest way to get your application up and running. Your application needs these libraries, so putting them in the same folder allows it to find them.

当然总有替代方案!

静态链接

为避免应用程序中必须存在大量dll,可以使用静态链接。您在链接时链接.lib文件,而不是在运行时链接到.dll文件。使您的.exe更大,但意味着您可以分发单个文件。

To avoid having a bunch of dlls that have to exist with your application, you could use static linking. You link the .lib files at link-time rather than linking to the .dll files at run-time. Makes your .exe larger, but means you may be able to distribute a single file.

将Dll放在其他文件夹中

dll通常不必与.exe放在同一文件夹中,尽管通常这是最合理的选择。 Windows在运行时查找.dll时会搜索多个文件夹,因此您可以将这些dll放在当前目录,系统目录(绝对不推荐)或 PATH 环境变量。实际上,不建议使用这些位置!将它们与.exe放在同一文件夹中是最安全的,因为通常您可以控制该文件夹。

The dlls do not have to be in the same folder as the .exe, though that normally makes the most sense. Windows will search several folders when looking for a .dll at run-time, so you could put the dlls in the current directory, a system directory (definitely not recommended), or another directory in your PATH environment variable. Actually none of those locations are recommended! Putting them in the same folder as the .exe is the safest, because generally you have control of that folder.

有关Windows如何搜索.dll的特定规则是此处概述: http://msdn.microsoft.com/ zh-TW / ca /​​ library / windows / desktop / ms682586(v = vs.85).aspx

The specific rules for how Windows searches for a .dll are outlined here: http://msdn.microsoft.com/en-ca/library/windows/desktop/ms682586(v=vs.85).aspx

自定义构建步骤

我不喜欢手动将文件放入调试或发布文件夹中。我喜欢将debug文件夹视为可以随时删除并重新构建的东西,并且我喜欢一个一步构建过程,将所有内容放置在需要的位置,以便可以在任何计算机上轻松构建。因此,我通常将创建一个自定义构建步骤,该步骤将所需的.dll从源文件夹(在源代码控制中)复制到我的输出文件夹中。

I don't like manually putting files in my debug or release folders. I like to think of the debug folder as something I can blow away and rebuild at any time, and I like to have a one-step build process that puts everything where it needs to be so I can easily build on any machine. So I will usually create a custom build step that copies the required .dlls from a "source" folder (in source control) into my output folder.

这篇关于在C ++项目中的位置以及如何包含dll文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆