如何将 C++Builder 应用程序拆分为 DLL
有人多次告诉我,我应该考虑将应用程序的各个部分拆分为单独的 DLL(以加快链接速度等),并且我正在尝试弄清楚它是如何工作的。
我知道我需要将 __declspec(dllexport) 添加到我计划使用的每个头文件声明中。这看起来很乏味,但它是可行的。
如何让应用程序 + DLL 运行?在一个简单的测试项目中,我发现唯一可行的方法是手动将 DLL 从 DLL 项目的构建输出目录复制到 exe 项目的构建输出目录。我知道我可以设置一个构建后步骤来执行此操作,但我希望 IDE 能够通过某种方式自动让应用程序项目在属于同一项目组时使用 DLL 项目。
如何调试应用程序 + DLL?我看到可以在 Project -> 下为 DLL 指定主机应用程序。选项->调试器,但到目前为止,我只能弄清楚如何一次调试一个项目。我真的希望能够在代码库中的任何位置设置断点,并单步执行代码库中的任何位置(而不是在项目边界处停止),但我不知道如何做到这一点。
I've been told several times that I should consider splitting parts of my app into separate DLLs (to speed up linking, etc.) and am trying to figure out how that works.
I understand that I need to add __declspec(dllexport)
to every header file declaration that I plan on using. That seems tedious, but it's doable.
How do I get the app + DLLs to run? In a simple test project, the only way I found that works is to manually copy the DLL from the DLL project's build output directory to the exe project's build output directory. I know I can set up a post-build step to do this, but I'd expect the IDE to have some way to automate having an app project use a DLL project when they're part of the same project group.
How do I debug the app + DLLs? I see where I can specify a host application for a DLL under Project -> Options -> Debugger, but so far, I've only been able to figure out how to debug one project at a time. I'd really like to be able to set breakpoints anywhere in the codebase and single-step through anywhere in the codebase (instead of being stopped at project boundaries), and I can't figure out how to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该做的是在 DLL 的头文件中创建一个 #define,当 DLL 项目编译头文件时,该头文件映射到 dllexport,并在其他项目中编译时映射到 dllimport。例如:
然后,您可以仅在 DLL 项目中定义 _BUILDING_DLL_,无论是在项目选项的条件列表中,还是在头文件的任何 #include 语句上方的代码中,例如:
DLL 项目生成一个 .lib 文件,用于静态链接到 DLL 的导出函数。您可以将该 .lib 文件添加到 EXE 项目中,然后像任何其他函数调用一样调用 DLL 函数。或者,您可以使用 Win32 API LoadLibrary() 和 GetProcAddress() 函数在运行时动态加载 DLL 函数,在这种情况下,您根本不使用 .lib 文件。
EXE 的文件夹是操作系统查找 DLL 的第一个位置,但它不是操作系统可以查找的唯一位置。 MSDN 记录了 DLL 在运行时如何定位:
动态链接库搜索顺序
仅仅成为同一项目组的一部分是不够的。这些项目是相互独立编译的。但是,您可以将 DLL 项目设置为 EXE 项目的依赖项(或者只需确保 DLL 项目在构建顺序上高于 EXE 项目),以便首先编译 DLL,然后使用 DLL 的 PostBuild 事件来移动在需要的地方编译编译的.lib和.dll二进制文件,最后将DLL的编译.lib文件添加到EXE项目中,以便在运行时使用DLL。
您有多种选择:
要仅调试 DLL 本身,请将 DLL 项目加载到 IDE 中,进入运行参数,然后在主机应用程序中设置已编译的 EXE。然后,您可以像运行 EXE 项目一样运行 DLL 项目。一旦 DLL 加载到内存中,EXE 将被执行,调试器将附加到 DLL。
要同时调试两个项目,请将 EXE 项目加载到 IDE 中,并确保在“项目选项”的“调试源”路径中指定 DLL 的源文件夹。然后,您可以正常运行 EXE 项目、在调用 DLL 函数时单步执行它们、在 DLL 源代码中设置断点等。
What you should do is create a #define in your DLL's header file that maps to dllexport when the header is compiled by the DLL project, and maps to dllimport when compiling in other projects. For example:
Then you can define _BUILDING_DLL_ in your DLL project only, either in the Conditionals list of the Project Options, or in your code above any #include statements for the header file, eg:
The DLL project generates a .lib file that is used for static linking to the DLL's exported functions. You can add that .lib file to your EXE project and then call the DLL functions like any other function call. Or you can dynamically load the DLL functions at runtime using the Win32 API LoadLibrary() and GetProcAddress() functions, in which case you do not use the .lib file at all.
The EXE's folder is the first place the OS looks for the DLL, but it is not the only place the OS can look. MSDN documents how DLL's are located at runtime:
Dynamic-Link Library Search Order
Just being part of the same project group is not enough. The projects are compiled independantly of each other. However, you can set the DLL project as a dependancy of the EXE project (or just make sure the DLL projct is higher up on the build order then the EXE project) so the DLL is compiled first, then use the DLL's PostBuild events to move the compiled .lib and .dll binaries where needed, and lastly add the DLL's compiled .lib file to the EXE project so the DLL is used at runtime.
You have a couple of choices:
To debug just the DLL by itself, load the DLL project into the IDE, go into the Run parameters, and set the compiled EXE at the Host application. You can then Run the DLL project as if it were an EXE project. The EXE will be executed and the debugger will attach to the DLL once it is loaded into memory.
To debug both projects at the same time, load the EXE project into the IDE instead, and make sure the DLL's source folder is specified in the Debug Souce path of the Project Options. You can then run the EXE project normally, step into the DLL functions when they are called, set breakpoints in the DLL's source, etc.