Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当我尝试使用异常时,为什么我的代码在Qt Creator中编译时会出现-fno异常?_C++_Qt - Fatal编程技术网

C++ 当我尝试使用异常时,为什么我的代码在Qt Creator中编译时会出现-fno异常?

C++ 当我尝试使用异常时,为什么我的代码在Qt Creator中编译时会出现-fno异常?,c++,qt,C++,Qt,在project.pro文件中,我指定了: QMAKE_CXXFLAGS += -fno-exceptions 但我可以在我的应用程序中抛出异常。有什么想法吗 示例:这不应该起作用,但它起作用了 #include <QApplication> #include <QtDebug> int main(int c, char**v) { QApplication app(c,v); try { throw 1; }

在project.pro文件中,我指定了:

QMAKE_CXXFLAGS += -fno-exceptions  
但我可以在我的应用程序中抛出异常。有什么想法吗

示例:这不应该起作用,但它起作用了

#include <QApplication>
#include <QtDebug>

int main(int c, char**v)
{
    QApplication app(c,v);
    try
    {
        throw 1;
    }
    catch(int i)
    {

    }
    return app.exec();
}
#包括
#包括
int main(int c,字符**v)
{
质量保证应用程序(c、v);
尝试
{
投掷1枚;
}
捕获(int i)
{
}
返回app.exec();
}

尝试使用以下两种方法

QMAKE_CFLAGS_RELEASE -= -fno-exceptions

QMAKE_CXXFLAGS_RELEASE -= -fno-exceptions

我被这个问题弄糊涂了

如果您有(或链接到)抛出异常的代码,您不能通过使用
-fno异常构建代码来神奇地消除异常。该标志对支持
try
catch
所需代码的生成的影响大于实际引发异常的代码

有关详细信息,请参阅。文件说:

总之,将异常处理的有效C++代码转换成无异常处理的方言。


因此,编译器似乎或多或少地导致异常处理(和抛出)代码“消失”,而不是检测其使用并标记错误。后者似乎是您所期望的,但这种期望是完全错误的。

您不能通过设置
QMAKE\u cxflags
来关闭异常,因为此选项由
CONFIG
处理。你应该使用

CONFIG-=exceptions
把它们关掉

当您既没有更改
QMAKE_CXXFLAGS
也没有更改
CONFIG
设置时,请参见g++参数:

g++ -c -O2 -frtti -fexceptions -mthreads -Wall <...> main.cpp
哦,我们得到了
-fno异常
CONFIG
-feexceptions
覆盖。 现在,让我们设置
CONFIG

g++ -c -O2 -frtti -Wall -fno-exceptions <...> main.cpp
mingw32-make.exe[1]: Leaving directory `G:/proj/ingeritance'
main.cpp: In function 'int qMain(int, char**)':
main.cpp:22:15: error: exception handling disabled, use -fexceptions to enable
mingw32-make.exe[1]: *** [release/main.o] Error 1
mingw32-make.exe: *** [release] Error 2
g++-c-O2-frti-Wall-fno异常main.cpp
mingw32 make.exe[1]:离开目录'G:/proj/ingeriance'
main.cpp:在函数“int qMain(int,char**)”中:
main.cpp:22:15:错误:异常处理已禁用,请使用-feexceptions启用
mingw32 make.exe[1]:***[release/main.o]错误1
mingw32-make.exe:**[release]错误2

哦!!编译错误

Qt库不使用异常.问题是什么?问题是,“为什么我的代码要编译,我希望它会失败,因为我错误地试图在我试图用-fno异常编译的代码中抛出异常?”?或者问题是,“如何使用Qt代码正确链接我的异常?”?“关于这一点的任何想法”都是不恰当的宽泛,因此,它不是在这里调查人们对给定文本的个人反应;-)@smallB所说的“我能够抛出异常”您的意思是它们被正确调度(
catch
正在工作,堆栈正在展开等),或者您能够编译并崩溃您的应用程序?@AzzA是的,我能够使用它们+1,可能比您链接的库文档更相关,这里是。
g++ -c -O2 -frtti -Wall -fno-exceptions <...> main.cpp
mingw32-make.exe[1]: Leaving directory `G:/proj/ingeritance'
main.cpp: In function 'int qMain(int, char**)':
main.cpp:22:15: error: exception handling disabled, use -fexceptions to enable
mingw32-make.exe[1]: *** [release/main.o] Error 1
mingw32-make.exe: *** [release] Error 2