异常处理在Windows上不能与Qt一起使用 [英] Exception handling doesn't work with Qt on Windows
问题描述
我面临着奇怪的问题。也就是说,Qt以某种方式关闭我的程序中的异常处理。我不能捕获任何异常,当我抛出异常应用程序崩溃。
I'm facing strange problem. Namely, Qt somehow turns off exception handling in my program. I can't catch any exception, and when I throw an exception application crashes.
我使用来自Qt SDK v2010.05的Qt 4.7.0(32位)在Windows 7(64位),g ++(GCC)4.5.1从MinGW ,NetBeans 6.9.1。
但是我也用g ++ 3.4.5(也来自MinGW)和Qt Creator 2.0.1 - 这个奇怪的行为。
I'm using Qt 4.7.0 (32 bit) from Qt SDK v2010.05 on Windows 7 (64 bit), g++ (GCC) 4.5.1 from MinGW, NetBeans 6.9.1. But I also cheked this with g++ 3.4.5 (also from MinGW) and Qt Creator 2.0.1 - same strange behavior.
例如(最简单的case ):
For example (simplest case):
#include <Qt/QApplication.h>
#include <iostream>
#include <stdexcept>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
try {
cout << "Before exception" << endl;
throw runtime_error("Exception occured");
cout << "After exception" << endl;
} catch (runtime_error& exc) {
cout << exc.what() << endl;
exit(1);
}
return 0;
}
当我执行上面的程序时,我得到这个输出:
When I execute above program I've got this output:
例外之前
Before exception
此应用程序已请求运行时以异常方式终止它。 br>
请与应用程序的支持小组联系以获取更多信息。
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
我试图添加标志-fexceptions
I've tried to add flag "-fexceptions" to g++ but it hasn't changed anything.
当我不使用Qt时,一切正常:
When I don't use Qt, everything is OK:
#include <Qt/QApplication.h> // It is not caused only by including Qt header
// so it doesn't matter if I comment this out or not
#include <iostream>
#include <stdexcept>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]) {
// QApplication app(argc, argv);
try {
cout << "Before exception" << endl;
throw runtime_error("Exception occured");
cout << "After exception" << endl;
} catch (runtime_error& exc) {
cout << exc.what() << endl;
exit(1);
}
return 0;
}
输出:
异常之前
异常发生
Before exception
Exception occured
有人知道为什么会发生这种情况那种方式和如何解决这个问题?是否与Qt构建时使用的异常处理方法类型(SJLJ或Dwarf-2)有关?
Does anybody know why is this happen that way and how to fix this? Has it something to do with type of exception handling method (SJLJ or Dwarf-2) used when Qt was build?
推荐答案
'res reconfigured and recompiled Qt with flag -exceptions
:
D:\Qt\2010.05\qt> mingw32 -make confclean&& configure -exceptions&&& mingw32-make
现在一切正常!
I've reconfigured and recompiled Qt with flag -exceptions
:
D:\Qt\2010.05\qt>mingw32-make confclean && configure -exceptions && mingw32-make
and now everything is ok!
感谢所有人的帮助,特别是对Nick D!
Thanks all for help, especially to Nick D!
反正很奇怪,我有Qt build没有这个标志。我从官方网站下载了二进制形式的Qt SDK。
Anyway, it's very strange that I had Qt build without this flag. I had downloaded Qt SDK in binary form from official site.
这篇关于异常处理在Windows上不能与Qt一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!