链接在库中的发布和.exe在调试崩溃在Visual studio [英] Linking against library in release and .exe in debug crashes in Visual studio

查看:233
本文介绍了链接在库中的发布和.exe在调试崩溃在Visual studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual C ++ 2008 SP1。我有一个在调试模式下编译的应用程序,但在发布模式下链接到库。



我在应用程序启动时崩溃。为了使问题更小,我创建了一个包含2个项目的简单解决方案:




  • lib_release(在发布模式下生成.lib) li>
  • exec_using_lib_release(在调试模式下生成.exe)



<足够有一个简单的类:

  // Foo.h 
#include< vector&
class Foo {
std :: vector< int> v;
public:
void doSomething();
};
//Foo.cpp
#includeFoo.h
void Foo :: doSomething(){}

'exec_using_lib_release'项目很简单:

  // main。 cpp 
#includeFoo.h
int main(){
Foo foo;
foo.doSomething();
return 0;
}

并且崩溃,这是 http:// stackoverflow。 com / questions / 746298 / how-do-you-build-a-debug-exe-msvcrtd-lib-against-a-release-built-lib-msvcrt-l ,但他的回答没有工作



我得到相同的链接器警告,我尝试了相同的步骤,但没有工作。有缺少的东西吗?



编辑:



(在发布模式下创建库),我使用多线程(/ MT),在exec_using_lib_release,我使用多线程调试(/ MTd)。我认为这是预期的做法,因为我想要创建没有调试信息的.lib。我在 MSDN运行时库中阅读了文档,这些是设置



我没有公共语言运行时支持。

解决方案

您不是使用相同的运行时发布和调试模块(但它有帮助),只要你遵循非常具体的规则:从来没有混合和匹配访问使用每个运行时分配的内存。



更简单地说,如果你在dll中有一个例程分配一些内存并返回到调用者,调用者必须永远不会释放它 - 你必须创建一个函数在原来的dll释放内存。



如果你认为Windows dll是建立只发布版本(除非你有Windows的调试版本),但你使用他们从你的调试应用程序,你会看到这是怎么回事。



现在你的问题是,你使用的是静态库,没有dll边界了, lib中的调用使用C运行时的静态版本进行编译。如果你的exe使用动态dll版本的运行时,你会发现链接器使用的那个,而不是你的静态库使用...,你会得到崩溃。



所以,你可以重建你的lib为dll;或者你可以确保他们都使用相同的CRT库;或者你可以确保他们都使用相同类型的CRT - 即dll版本或静态版本,同时保持调试/发布差异。



至少,我认为这是你的问题 - 什么是代码生成,运行时库设置?


I'm using Visual C++ 2008 SP1. I have an app that is compiled in debug mode, but links against a library in release mode.

I'm getting a crash at the start-up of the application. To make the problem smaller, I created a simple solution with 2 projects:

  • lib_release (generates a .lib, in release mode)
  • exec_using_lib_release (genereates a .exe, in debug mode)

The 'lib_release' project is simple enough to have a simple class:

//Foo.h
#include <vector>
class Foo {
  std::vector<int> v;
  public:
  void doSomething();
};
//Foo.cpp
#include "Foo.h"
void Foo::doSomething() {}

The 'exec_using_lib_release' project is simple like this:

//main.cpp
#include "Foo.h"
int main() {
   Foo foo;
   foo.doSomething();
   return 0;
}

And it crashes, it's the same problem reported by http://stackoverflow.com/questions/746298/how-do-you-build-a-debug-exe-msvcrtd-lib-against-a-release-built-lib-msvcrt-l, but his answer didn't work for me.

I get the same linker warnings, I tried the same steps, but none worked. Is there something I'm missing?

EDIT:

On the lib_release (that creates a library in release mode), I'm using Multi Threaded (/MT), and at the exec_using_lib_release, I'm using Multi Threaded Debug (/MTd). I think this is the expected way of doing it, since I want the .lib to be created without debug info. I read the document at MSDN Runtime library and those are the settings of linking against the CRT in a static way.

I don't have 'Common Language Runtime Support' either.

解决方案

You don't have to use the same runtimes for release and debug modules (but it helps), as long as you follow very specific rules: never mix and ,match accessing the memory allocated using each runtime.

To put this more simply, if you have a routine in a dll that allocates some memory and returns it to the caller, the caller must never free it - you must create a function in the original dll that frees the memory. That way you're safe from runtime mismatches.

If you consider that the Windows dlls are built release only (unless you have the debug version of Windows), yet you use them from your debug applications, you'll see how this matters.

Your problem now is that you're using a static library, there is no dll boundary anymore, and the calls in the lib are compiled using the static version of the C runtime. If your exe uses the dynamic dll version of the runtime, you'll find that the linker is using that one instead of the one your static lib used... and you'll get crashes.

So, you could rebuild your lib as a dll; or you could make sure they both use the same CRT library; or you could make sure they both use the same type of CRT - ie the dll version or the static version, whilst keeping debug/release differences.

At least, I think this is your problem - what are the 'code generation, runtime library' settings?

这篇关于链接在库中的发布和.exe在调试崩溃在Visual studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆