简体   繁体   English

如何在我的dll中集成第三方库?

[英]How to integrate third-party libraries in my dll?

I've a MSVS solution with two C++ projects. 我有两个C ++项目的MSVS解决方案。 Project A is a DLL linked to third-party libraries (tp1.lib and tp2.lib) and it is referenced by project B, the exe. 项目A是链接到第三方库(tp1.lib和tp2.lib)的DLL,并且被项目B(即exe)引用。 Everything compiles properly, but when I run B.exe I get the error for tp1.dll missing, while I would expect that the relevant portion of the code inside the third-party libraries should have been pulled into my A.dll. 一切都可以正确编译,但是当我运行B.exe时,我会丢失tp1.dll的错误,而我希望第三方库中代码的相关部分应该已被拉到我的A.dll中。

Is this my assumption wrong? 这是我的假设错误吗? If not, I would need for you to know which settings can cause this behaviour. 如果没有,我将需要您知道哪些设置可能导致此行为。 Among other settings, these are those for Project A under Properties>ConfigurationProperties that I guess are relevant: 在其他设置中,这些是我认为相关的Properties> ConfigurationProperties下的Project A的设置:

  • General>Configuration Type: Dynamic Library 常规>配置类型:动态库
  • C/C++>Additional Include Directories: %path to third-party include files (.h)% C / C ++>其他包含目录:第三方包含文件的路径(.h)%
  • Linker>General>Additional Library Directories: %path to the third-party libraries (.lib)% 链接器>常规>其他库目录:第三方库(.lib)的路径%
  • Linker>Input>Additional Dependencies: tp1.lib; 链接器>输入>其他依赖关系:tp1.lib; tp2.lib tp2.lib

Then, in Ah I have: 然后,在Ah中,我有:

#include "tp1.h"
#include "tp2.h"

and in A.cpp 并在A.cpp中

#include "stdafx.h"
#include "A.h"

It seems that tp1.lib and tp2.lib are import libraries for tp1.dll and tp2.dll . 看来,tp1.libtp2.lib导入库对于tp1.dlltp2.dll。 They do not contain any real code, only some helpers to redirect all the calls you make to those libraries to the proper addresses in the DLL. 它们不包含任何实际代码,只有一些帮助程序将您对这些库的所有调用重定向到DLL中的正确地址。

When you link against an import library of a DLL, the DLL is not included into resulting binary. 当您链接到DLL的导入库时,该DLL 包括在生成的二进制文件中。 That's why the resulting binary needs the DLL file at the moment it is launched: it searches for it at startup, and crashes if it is not found. 这就是为什么生成的二进制文件在启动时就需要DLL文件:它在启动时进行搜索,如果找不到则崩溃。

If you want to add all the code from libraries tp1 and tp2 into your binary, you have to rebuild them as a static libraries (without any DLLs). 如果要将来自库tp1tp2的所有代码添加到二进制文件中,则必须将它们重建为静态库(没有任何DLL)。 Then linking against tp1.lib and tp2.lib would work as you expect. 然后,按您的预期进行链接到tp1.libtp2.lib

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

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