当我尝试使用异常时,为什么我的代码在Qt Creator中使用-fno-exceptions进行编译? [英] Why does my code compile with -fno-exceptions in Qt Creator when I try to use exceptions?

查看:41
本文介绍了当我尝试使用异常时,为什么我的代码在Qt Creator中使用-fno-exceptions进行编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In the project .pro file I've specified:

QMAKE_CXXFLAGS += -fno-exceptions  

但是我可以在我的应用程序中引发异常.有什么想法吗?

Yet I'm able to throw exceptions in my app. Any thoughts on that?

示例:这应该行不通,但是行之有效

Example: This shouldn't work, but it works

#include <QApplication>
#include <QtDebug>

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

    }
    return app.exec();
}

推荐答案

您不会通过设置 QMAKE_CXXFLAGS 来关闭异常,因为此选项由 CONFIG 处理.您应该使用

You do not turn off exceptions by setting QMAKE_CXXFLAGS because this options is handled by CONFIG. You should use

CONFIG-=exceptions

将其关闭.

在未更改 QMAKE_CXXFLAGS CONFIG 设置的情况下,请参见g ++参数:

See the g++ args when you have neither QMAKE_CXXFLAGS nor CONFIG settings changed:

g++ -c -O2 -frtti -fexceptions -mthreads -Wall <...> main.cpp

现在,让我们设置 QMAKE_CXXFLAGS :获取

Now, let's set QMAKE_CXXFLAGS: get

g++ -c -fno-exceptions -O2 -frtti -fexceptions -mthreads -Wall <...> main.cpp

糟糕,我们的 -fno-exceptions CONFIG -fexceptions 覆盖.现在,让我们设置 CONFIG :

Ooops, we get our -fno-exceptions is overridden by CONFIG's -fexceptions. Now, let's set 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

哦!编译错误!

这篇关于当我尝试使用异常时,为什么我的代码在Qt Creator中使用-fno-exceptions进行编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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