如何构建 C++项目将所有动态链接库嵌入到exe文件中?
大家好,我正在开始开发一个 C++ 项目,在这个项目中我必须使用一些有几个 dll 文件的开源项目。然后我有一个问题“如何构建C++项目在exe文件中嵌入所有动态链接库?” 感谢您的帮助!
注意:抱歉,我忘记了我在 x86 上使用 Visual Studio 编译器
everybody, I am getting started develop a C++ project and in this project I must use some opensource project have several dll file. Then I have a question "How to build C++ project embed all dynamic link library in exe file?"
Thank for help!
Note: Sorry, I forgot that I'm using visual studio compiler on x86
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题没有通用答案。这取决于您是否需要跨平台。
但是,既然您提到“x86 上的 Visual Studio 编译器”,我敢打赌您的目标是 Windows。在这种情况下,您有两种选择:
官方推荐的方式:将 dll 作为资源嵌入到可执行文件中;然后当您的程序启动时,您将这些 dll 作为临时文件提取到磁盘上的某个位置(注意文件权限),然后您使用
的黑客方式:你写PE 可执行加载程序,以避免 Windows API 只提供从磁盘上的文件
LoadLibrary
的方式这一事实。通过编写自己的 PE 加载程序,您将能够直接从内存流加载 dll。再说一次,我只是提一下它作为参考,但这绝不是人们应该做的事情最后,您需要遵守您正在使用的那些开源项目所选择的许可证。我的回答提供了有关如何实现目标的技术指导;这并不意味着您正在使用的项目的许可证允许您这样做。
There is no general answer to your question. It depends whether you need it to be cross platform or not.
However, since you're mentioning "visual studio compiler on x86", I bet you're targeting Windows. In such a case you have two options:
the official and recommended way: embed your dlls as resources in your executable; then when your program starts you extracts these dlls somewhere on the disk as temporary files (beware file permissions) then you use
LoadLibrary
+GetProcAddress
the hackish way: you write a PE executable loader in order to circumvent the fact that the Windows API only offers a way to
LoadLibrary
from a file on disk. By writing your own PE loader you'll be able to load a dll directly from a memory stream. Then again, I'm just mentioning it for reference but it's by no means something one should doFinally, you need to comply to the license chosen by those opensource projects you're using. My answers gives technical directions about how to achieve your goal; it doesn't mean the license of the project you're using allows you to do so.