转换 C++项目目前已开发为 DLL 中的独立可执行文件

发布于 2024-11-06 19:37:53 字数 425 浏览 0 评论 0原文

(我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

滴情不沾 2024-11-13 19:37:53

老实说,我会看一下 DLL 导出docs 并选择最适合您的导出方法。在任何情况下,您都可以简单地从客户端应用程序中按名称引用导出的函数,就像使用静态库一样。

当您将项目构建为 DLL 时,IDE 将生成

  1. 运行时的 DLL 文件和
  2. 包含导出的函数解析信息的 LIB 文件 - 这就是您链接的文件。

根据定义,您不能静态链接 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

  1. The DLL file for runtime and
  2. a LIB file containing exported function resolution information - that's the one you link against.

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).

盛夏尉蓝 2024-11-13 19:37:53

由于您使用的是 C++,我假设您正在导出类(?)。 CodeProject 上有一个非常好的示例,它引导您完成几个选项。其中最简洁的是使用抽象接口:

C++ 抽象接口(即仅包含纯虚方法且不包含数据成员的 C++ 类)试图充分利用两个世界:独立于编译器的干净对象接口,以及方便的面向对象的方法方式来电。所需要做的就是提供一个带有接口声明的头文件并实现一个将返回新创建的对象实例的工厂函数。只有工厂函数必须使用 __declspec(dllexport/dllimport) 说明符进行声明。该接口不需要任何额外的说明符。

抽象接口示例如何工作的示例

您无法静态链接到动态链接库 。如果您想静态链接,请创建一个 .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:

A C++ abstract interface (i.e., a C++ class that contains only pure virtual methods and no data members) tries to get the best of both worlds: a compiler independent clean interface to an object, and a convenient object oriented way of method calls. All that is required to do is to provide a header file with an interface declaration and implement a factory function that will return the newly created object instances. Only the factory function has to be declared with the __declspec(dllexport/dllimport) specifier. The interface does not require any additional specifiers.

Example of how the abstract interface example works

You can't statically link to a Dynamic Link Library. If you want to link statically, create a .lib instead.

不顾 2024-11-13 19:37:53
  • 要使用您的 DLL,您必须 #include 与您的 dll/lib 关联的头文件,并链接到与您的 .dll 关联的 .lib 文件

  • 您需要 _declspec(dllexport)/_declspec(dllimport)指示您要导出/导入 dll 的内容。这可以轻松完成,如下所示

<前><代码> #ifdef FOO_EXPORTS
#define EXPORT_ME __declspec(dllexport)
#别的
#define IMPORT_ME __declspec(dllimport)
#endif

在 dll 的标头中,您只需 #define FOO_EXPORTS 并放置 EXPORT

foo.hpp

class EXPORT_ME foo2();

void EXPORT_ME foo_funct(foo2 *foo_ptr);

,任何需要使用导出项的文件只需调用 foo.hpp 中定义的方法标头(默认行为是导入)

use_foo.cpp

main()
{
      #include "foo.cpp";
foo2 myfoo;
foo_funct(&my_foo);
}
  • 正如其他人所说,lib 是静态链接的,而 dll 是动态链接的。静态链接时的任何引用元素都会在编译时内联放置到源中,并且通常会生成较大的程序(就文件大小而言),而动态链接的元素在运行时链接,因此文件大小通常较小。静态与动态还有许多其他优点/缺点 - 我建议您关注 Doc Browns 链接以获取更多信息
  • 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

 #ifdef FOO_EXPORTS
      #define EXPORT_ME __declspec(dllexport)
 #else
      #define IMPORT_ME __declspec(dllimport)
 #endif

in the headers for your dll you simply need to #define FOO_EXPORTS and place the EXPORT

foo.hpp

class EXPORT_ME foo2();

void EXPORT_ME foo_funct(foo2 *foo_ptr);

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

main()
{
      #include "foo.cpp";
foo2 myfoo;
foo_funct(&my_foo);
}
  • As others have said, a lib is statically linked and a dll is dynamically linked. Any referenced elements when linked statically are placed in-line at compile time into your source and generally produce a larger program (as far as file size), while dynamically linked elements are linked in at run-time so the file size is usually smaller. There are many other pros/cons to static vs dynamic - I recommend you follow Doc Browns link for more info
他不在意 2024-11-13 19:37:53

切换到 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文