转换 C++项目目前已开发为 DLL 中的独立可执行文件
(我在 Windows 7 64 位计算机上使用 Microsoft Visual Studio 2010)
我开发了一个 C++ 程序,它更像是一个库,随着时间的推移变得相当复杂。它现在确实可以作为一个简单的可执行文件运行,但我想将其转换为 DLL,以便其他程序可以轻松访问该功能。
我在使用 DLL 方面完全没有经验,但我想避免在此过程中进行大量额外的工作和代码更改。
我知道我可以将编译目标选择为“DLL”,但我感觉仅靠它是无法完成这项工作的。
如果我成功地将我的项目编译成DLL文件,我如何从可执行项目中使用其中的函数?
我可以避免使用 _dllexport 并按名称导入每个函数吗?
如何静态链接 DLL,这样做的(缺点)优点是什么?
(I'm using Microsoft Visual Studio 2010 on a Windows 7 64 bit machine)
I have developed a C++ program that is more of a library which became quite complex over time. It does right now work as a simple executeable, but I'd like to convert it into a DLL so the functionality can be accessed by other programs easily.
I'm not at all experienced in working with DLLs, but I want to avoid much additional work and code changes in the process.
I know that I can select the compile target to "DLL", but I have the feeling that alone won't do the job.
If I successfully compiled my project into a DLL file, how do I use the functions in it from an executable project?
Can I avoid using _dllexport and importing every function per-name?
How does one statically link a DLL, and what are the (dis)advantages of this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
老实说,我会看一下 DLL 导出docs 并选择最适合您的导出方法。在任何情况下,您都可以简单地从客户端应用程序中按名称引用导出的函数,就像使用静态库一样。
当您将项目构建为 DLL 时,IDE 将生成
根据定义,您不能静态链接 DLL(即动态链接库) - 相反,您链接到从 DLL 导出函数的库,然后 DLL 在运行时加载,或者在进程上自动加载启动或按需启动。也可以完全按需加载 DLL,无需任何静态链接(请参阅 LoadLibraryEx 等)。
Honestly, I would take a look at the DLL export docs and pick whatever export method works best for you. In any case, you can simply reference exported functions by name from your client apps, as you would with a static library.
When you build the project as a DLL, the IDE will generate
By definition, you cannot statically link a DLL (that's DYNAMIC link library) - instead, you link to a library that exports the functions from the DLL, and then the DLL loads at runtime, either automatically on process start or on demand. It's also possible to load the DLL completely on demand without any static linkage (see LoadLibraryEx etc).
由于您使用的是 C++,我假设您正在导出类(?)。 CodeProject 上有一个非常好的示例,它引导您完成几个选项。其中最简洁的是使用抽象接口:
您无法静态链接到动态链接库 。如果您想静态链接,请创建一个 .lib。
Since you're using C++ I'm assuming you're exporting classes(?). There's a really good example over on CodeProject which walks you through a few options. The cleanest of which is to use an abstract interface:
You can't statically link to a Dynamic Link Library. If you want to link statically, create a .lib instead.
要使用您的 DLL,您必须 #include 与您的 dll/lib 关联的头文件,并链接到与您的 .dll 关联的 .lib 文件
您需要 _declspec(dllexport)/_declspec(dllimport)指示您要导出/导入 dll 的内容。这可以轻松完成,如下所示
在 dll 的标头中,您只需 #define FOO_EXPORTS 并放置 EXPORT
foo.hpp
,任何需要使用导出项的文件只需调用 foo.hpp 中定义的方法标头(默认行为是导入)
use_foo.cpp
To use your DLL you have to #include the header file(s) associated with your dll/lib and link with the .lib file that is associated with your .dll
You need the _declspec(dllexport)/_declspec(dllimport) to indicate you want to export/import the contents of the dll. This can be easily accomplished as follows
in the headers for your dll you simply need to #define FOO_EXPORTS and place the EXPORT
foo.hpp
and any file that needs to use the exported items simply needs to call the the methods defined in the foo.hpp header (the default behavior is to import)
use_foo.cpp
切换到 MinGW 下的 gcc。构建和链接到 DLL 就像构建和链接到静态库一样简单。它甚至可以透明地处理 C++ 名称修改(但调用程序也需要使用 gcc 进行编译)。
Switch to gcc under MinGW. Building and linking to a DLL is just as easy as building and linking to a static library. It even handles C++ name mangling transparently (but then the calling program also needs to be compiled with gcc).