C++ 如何在exe中包含所有dll?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23844887/
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-28 00:33:57  来源:igfitidea点击:

How to include all dll's in exe?

c++opencvvisual-studio-2012dllexe

提问by Pavlo Zvarych

I have a Visual Studio 12 project; source code written in C++; it's an OpenCV project. I want to give my compiled program to someone else, but, on other PC, I getting an error message about missing some dlls. My program using many OpenCV (maybe, not only) dll's. How can I resolve that problem?Maybe in VS 12 available an option to include all dll's in .exe?It's a pretty similar question without proper answer: include dlls in visual studio c++ 2008

我有一个 Visual Studio 12 项目;用C++编写的源代码;这是一个 OpenCV 项目。我想将我编译的程序提供给其他人,但是,在其他 PC 上,我收到一条关于缺少某些 dll 的错误消息。我的程序使用了许多 OpenCV(也许,不仅是)dll。我该如何解决这个问题?也许在 VS 12 中可以选择将所有 dll 包含在 .exe 中?这是一个非常相似的问题,没有正确答案: 在 Visual Studio C++ 2008 中包含 dll

回答by datenwolf

DLLs themself can not be "statically linked" into an executable; that completely defies their purpose (well, actually you can use some really weird voodoo tricks to actually do it, but this is neither recommendable nor should you try it if you have to ask this question).

DLL 本身不能“静态链接”到可执行文件中;这完全违背了他们的目的(好吧,实际上你可以使用一些非常奇怪的伏都教技巧来实际做到这一点,但这既不值得推荐,也不应该在你不得不问这个问题时尝试)。

The simple solution would be to identify all the DLLs your program requires (just starting the program in the Debugger will generate a log file listing them all) and copy those DLLs into the same directory as the EXE resides in; as it happens the directory with the EXE file in is also the first directory where the system looks for DLLs before advancing to the standad system directoris in default configuration. Package it up and distribute it that way.

简单的解决方案是识别您的程序需要的所有 DLL(只需在调试器中启动程序将生成一个列出所有这些的日志文件)并将这些 DLL 复制到 EXE 所在的同一目录中;碰巧的是,EXE 文件所在的目录也是系统在进入默认配置的标准系统目录之前查找 DLL 的第一个目录。将其打包并以这种方式分发。

回答by berak

the more complicated solution would be, to build static opencv libraries from src, then link your program against those, resulting in 1 large binary exe-chunk, that does not use any dlls (apart from ffmpeg, not sure about that one).

更复杂的解决方案是,从 src 构建静态 opencv 库,然后将您的程序链接到这些库,从而产生 1 个大的二进制 exe-chunk,它不使用任何 dll(除了 ffmpeg,不确定那个)。

to build static libs, you'd need to run cmake with : BUILD_SHARED_LIBS=OFF

要构建静态库,您需要使用以下命令运行 cmake:BUILD_SHARED_LIBS=OFF

but take a deep breath, before doing that. linking your program will be sigificantly more difficult, because now you have to link all the zlib,libpng, whatever dependecies manually ( which before got conveniently linked into your dlls )

但在此之前深吸一口气。链接您的程序将更加困难,因为现在您必须手动链接所有 zlib、libpng 和任何依赖项(之前可以方便地链接到您的 dll 中)

again, the most simple solution is to deploy all the opencv dlls with your program.

同样,最简单的解决方案是将所有 opencv dll 与您的程序一起部署。

回答by Ferruccio

You can use the Windows Dependency Walkerto determine which DLLs your program needs to run.

您可以使用Windows Dependency Walker来确定您的程序需要运行哪些 DLL。

Actually, this only tells you which DLLs your program needs to launch successfully. If you load DLLs dynamically (via LoadLibrary) then you are on your own.

实际上,这只会告诉您您的程序需要哪些 DLL 才能成功启动。如果您动态加载 DLL(通过 LoadLibrary),那么您就靠自己了。

回答by Hazem

If you opt for the accepted solution (package the DLLs with the EXE file), and you don't want to go into the trouble of finding which DLLs to use, then you can copy all the OpenCV DLLs. They're not so big (65 MB on OpenCV 2.43). They are located at ...\opencvXXX\build\x64\vc10\bin\

如果您选择接受的解决方案(将 DLL 与 EXE 文件打包),并且您不想陷入寻找要使用的 DLL 的麻烦,那么您可以复制所有 OpenCV DLL。它们不是那么大(在 OpenCV 2.43 上为 65 MB)。他们位于...\opencvXXX\build\x64\vc10\bin\

where XXXis the OpenCV version. You can use x64or x86depending on your platform (32 or 64-bit). And the version of vccan be different on your system (vc9, vc10, etc...)

XXXOpenCV 版本在哪里。您可以使用x64x86取决于您的平台(32 位或 64 位)。和版本vc可以是你的系统上的不同(vc9vc10,等...)