gpt4 book ai didi

c++ - 异常处理不适用于 Windows 上的 Qt

转载 作者:可可西里 更新时间:2023-11-01 12:43:33 25 4
gpt4 key购买 nike

我遇到了一个奇怪的问题。也就是说,Qt 以某种方式关闭了我程序中的异常处理。我无法捕获任何异常,当我抛出异常时应用程序崩溃。

我在 Windows 7(64 位)上使用来自 Qt SDK v2010.05 的 Qt 4.7.0(32 位),来自 MinGW 的 g++ (GCC) 4.5.1,NetBeans 6.9.1。但我也用 g++ 3.4.5(也来自 MinGW)和 Qt Creator 2.0.1 对此进行了检查——同样奇怪的行为。

例如(最简单的情况):

#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;
}

当我执行上面的程序时,我得到了这个输出:

Before exception

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

我尝试向 g++ 添加标志“-fexceptions”,但它没有改变任何东西。

当我不使用Qt时,一切正常:

#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)有关吗?

最佳答案

我已经使用标记-exceptions 重新配置并重新编译了 Qt:
D:\Qt\2010.05\qt>mingw32-make confclean && configure -exceptions && mingw32-make
现在一切都好!

感谢大家的帮助,尤其是 Nick D!

无论如何,很奇怪我在没有这个标志的情况下构建了 Qt。我已经从官方网站下载了二进制形式的 Qt SDK。

关于c++ - 异常处理不适用于 Windows 上的 Qt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4149118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com