Take the 2-minute tour ×

I'm coding Qt/C++ in Netbeans 7.0 and i want enable -O3 compiler switch for my project. The default optimization level of building a project is -O2 in Netbeans.

I added -O3 in this way:

Properties > Build > Qt > Expert > Custom Definitions > Add QMAKE_CXXFLAGS += -O3.

but when rebuilding the project i see this for each .cpp file:

g++.exe -c -O3 -O2 -frtti -fexceptions -mthreads -Wall ...... test.cpp

and i think the first -O3 is dropped by second -O2, and the optimization level -O2

how can i configure my project through Properties > Build > .... for -O3? or, how can i remove -O2?

share|improve this question
You are right. See the documentation, which reads "If you use multiple -O options, with or without level numbers, the last such option is the one that is effective." –  Alexandre C. Oct 17 '11 at 17:19
Some random forum posts say "project properties -> Build -> C++ Compiler -> Additional Options". No idea if they're right. –  Mooing Duck Oct 17 '11 at 17:22
@MooingDuck: No, that ways is valid for "C/C++ Application" projects, not "C/C++ Qt Application". –  M M. Oct 17 '11 at 17:31
add comment

3 Answers

up vote 3 down vote accepted

I assume you need -O3 for release build only, right?

Then setting QMAKE_CXXFLAGS_RELEASE += -O3 puts -O3 ater -O2, at least in my command line build in Debian and MacOS X.

That would make sense, since -O2 is provided by QMAKE_CXXFLAGS_RELEASE in platform-specific mkspecs, so appending anything should work like this.

share|improve this answer
Assuming you're using win32-g++ specs, your QMAKE_CFLAGS_RELEASE (and QMAKE_CXXFLAGS_RELEASE) are equal to "-O2". Can you give it a try and put QMAKE_CXXFLAGS_RELEASE = -O3 (= instead of +=)? –  ayoy Oct 17 '11 at 17:37
+1: Yes it works, but how about QMAKE_CXXFLAGS ? –  M M. Oct 17 '11 at 17:39
Out of curiosity, why would you need optimizations for debug build? This might cause issues with debugging... I don't know whether Netbeans add its own logic to Qt mkspecs, but if you shouldn't be getting -O2 for debug build. So maybe Netbeans treats every build as a release build. Anyways, the simplified command line looks like this: g++ $${QMAKE_CXXFLAGS} $${QMAKE_CXXFLAGS_RELEASE} (...), so whatever you put in QMAKE_CXXFLAGS can be overwritten by QMAKE_CXXFLAGS_RELEASE. –  ayoy Oct 17 '11 at 17:46
So, there is no way to remove default QMAKE_CXXFLAGS options without overwriting them ? –  M M. Oct 17 '11 at 18:00
In your Qt installation, look for mkspecs folder, and check win32-g++\qmake.conf file. Default build settings are provided there. QMAKE_CXXFLAGS is empty for that mkspecs. Anyway, to remove default flags you have to overwrite them with an empty string, I think that's pretty clear. –  ayoy Oct 17 '11 at 18:03
add comment

Add two entries in the Custom Definitions box: one with QMAKE_CXXFLAGS -= -O2 and another with QMAKE_CXXFLAGS += -O3. This will remove the -O2 flag and add the -O3 flag.

share|improve this answer
add comment

The way to get Netbeans to use -O3 is to go into Project properties > Build > C++ Compiler, then set Development Mode to "Performance Release"

share|improve this answer
add comment

Your Answer


