Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my code compile with -fno-exceptions in Qt Creator when I try to use exceptions?

Tags:

c++

qt

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();
}
like image 583
smallB Avatar asked Jan 10 '12 12:01

smallB


People also ask

How do you handle exceptions in Qt?

Qt has caught an exception thrown from an event handler. Throwing exceptions from an event handler is not supported in Qt. You must not let any exception whatsoever propagate through Qt code. If that is not possible, in Qt 5 you must at least re-implement QCoreApplication::notify() and catch all exceptions there.

Does Qt throw exception?

Qt itself will not throw exceptions. Instead, error codes are used. In addition, some classes have user visible error messages, for example QIODevice::errorString() or QSqlQuery::lastError(). This has historical and practical reasons - turning on exceptions can increase the library size by over 20%.

Is Qt Creator a compiler?

Qt Creator uses the C++ compiler from the GNU Compiler Collection on Linux. On Windows it can use MinGW or MSVC with the default install and can also use Microsoft Console Debugger when compiled from source code.

What is the difference between Qt and Qt Creator?

Qt creator can't build and debug any code (except perhaps, you want to use it for other reasons) without Qt because Qt contains the necessary tools for that purpose. So download Qt Creator alone if you already have Qt or want to update your old Qt creator and download Qt 5.4.


2 Answers

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

CONFIG-=exceptions

to turn them off.

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

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

Now, let's set QMAKE_CXXFLAGS: get

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

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

Oh! compilation error!

like image 146
Lol4t0 Avatar answered Oct 01 '22 02:10

Lol4t0


I'm a bit confused by the question.

If you have (or link to) code that throws exceptions, you can't just magically make them go away by building your code with -fno-exceptions. The flag affects the generation of code necessary to support try and catch, more than the code that actually throws exceptions.

See GCC documentation for details. The documentation says:

In sum, valid C++ code with exception handling is transformed into a dialect without exception handling.

So, it seems more as if the compiler more or less causes the exception-handling (and throwing) code to "go away", rather than detect its use and flag an error. The latter seems to be what you're expecting, but that expectation is, then, simply wrong.

like image 29
unwind Avatar answered Oct 01 '22 00:10

unwind