简体   繁体   English

使用发行版EXE调试DLL

[英]Debug DLL with release EXE

Is it possible to execute debug mode DLL with release mode EXE? 是否可以使用发布模式EXE执行调试模式DLL?

I am trying this scenario but EXE does not load the debug DLL and throws error "This application has failed to start...". 我正在尝试这种情况,但EXE不加载调试DLL并抛出错误“此应用程序无法启动...”。

I know this is not good scenario to do but Due to certain requirements I have to make this work. 我知道这不是好事,但由于某些要求,我必须做到这一点。

It can work if your dll interface has no dependencies on classes that may look different in debug and release. 如果您的dll接口对调试和发布中可能看起来不同的类没有依赖关系,它可以工作。 eg std::string and std::vector in MSVC are not compatible in debug and release. 例如,MSVC中的std :: string和std :: vector在调试和发布中不兼容。 (Fences ...) (栅栏......)

So for example 所以举个例子

std::string GetName();

will not work. 不管用。

In additional new and delete should not be shifted because debug/release use different runtimes. 另外new和delete不应该被移位,因为调试/发布使用不同的运行时。 But anyway you should always delete in the same context(dll/exe) as new . 但无论如何,你应该总是delete在同样的情况下(DLL / EXE)作为new

Yes, this can work. 是的,这可以工作。

Your "application has failed to start" issue is most likely you copied a debug build of the DLL (built on your machine with Visual Studio), to a machine that did not have the DEBUG CRT installed. 您的“应用程序无法启动”问题很可能是您将DLL的调试版本(使用Visual Studio在您的计算机上构建)复制到未安装DEBUG CRT的计算机上。 Usually copying over MSVCRTD(version).dll to the same directory as your program files solves this problem. 通常将MSVCRTD(版本).dll复制到与程序文件相同的目录中可以解决此问题。 I have a previous answer that covers some of this here . 我有一个以前的答案,涵盖一些这这里

Best bet is to always have all your binaries linked to the same dynamic MSVCRT DLL so they all share the same runtime. 最好的办法是始终将所有二进制文件链接到相同的动态MSVCRT DLL,以便它们共享相同的运行时。

Another easy workaround is to compile your DEBUG DLL to use the same flavor of the MSVCRT DLL (or statically link to the CRT). 另一个简单的解决方法是编译您的DEBUG DLL以使用相同的MSVCRT DLL(或静态链接到CRT)。 Somewhere in the VS project propery pages (code generation I think) is the dropdown for choosing the CRT. VS项目中的某个地方(我认为代码生成)是选择CRT的下拉列表。 There's nothing wrong with linking the retail MSVCRT into a debug DLL - or statically linking. 将零售MSVCRT链接到调试DLL或静态链接没有任何问题。

The things to watch out for are when you have a different flavor of the debug C runtime linked to the different binaries. 需要注意的是,当您将不同类型的调试C运行时链接到不同的二进制文件时。 If you have the release MSVCRT dll linked for the EXE, but the debug MSCVRTD DLL for the DLL, that can cause problems in a few circumstances. 如果你有发布的MSVCRT dll为EXE链接,但是为DLL调试MSCVRTD DLL,那可能会在一些情况下引起问题。 This is because handles and memory blocks are tracked by two different instances of the CRT. 这是因为句柄和内存块由两个不同的CRT实例跟踪。

Examples: 例子:

  1. If you allocate memory in the EXE, but free in in the DLL. 如果在EXE中分配内存,但在DLL中释放。 And vice versa. 反之亦然。

  2. File handles opened with fopen() in the EXE, but used or closed in the EXE (and vice versa). 在EXE中使用fopen()打开文件句柄,但在EXE中使用或关闭(反之亦然)。

  3. For any of the header files for the DLL's interface, having any sort of inline functions or methods implemented in the header file is an easy way to cause #1 or #2 to happen. 对于DLL接口的任何头文件,在头文件中实现任何类型的内联函数或方法都是导致#1或#2发生的简单方法。

  4. Sharing STL objects (std::string, std::list, std::vector) is a definite no-no for mixed CRT usage. 共享STL对象(std :: string,std :: list,std :: vector)对于混合CRT使用是一个明确的禁忌。

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

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