Adding extra compiler option in Qt [duplicate] - c++

This question already has answers here:
Where in Qt Creator do I pass arguments to a compiler?
(6 answers)
Closed 8 years ago.
Where in Qt can I specify additional compiler options? Like for example -std=c++0x?

You can try adding
QMAKE_CXXFLAGS += -std=c++0x
to your .pro file.
However, this should not be used in Qt 5 for enabling specific c++ standard. Instead, c++11 or c++14 in CONFIG variable to do that. It will enable GNU extensions (-std=gnu++11), but if that is unwanted, also add strict_c++ if you want to disable those. For example, this should pass -std=c++11 to the compiler:
CONFIG += c++11 strict_c++

In your .pro file, you could add:
QMAKE_CXXFLAGS += -std=c++0x
I think every variable in the spec's qmake.conf can be altered like that.
For example, the win32-g++ spec has, among other variables, these:
QMAKE_CC = gcc
QMAKE_LEX = flex
QMAKE_LEXFLAGS =
QMAKE_YACC = byacc
QMAKE_YACCFLAGS = -d
QMAKE_CFLAGS =
QMAKE_CFLAGS_DEPS = -M
QMAKE_CFLAGS_WARN_ON = -Wall
QMAKE_CFLAGS_WARN_OFF = -w
QMAKE_CFLAGS_RELEASE = -O2
QMAKE_CFLAGS_DEBUG = -g
QMAKE_CFLAGS_YACC = -Wno-unused -Wno-parentheses
QMAKE_CXX = g++
QMAKE_CXXFLAGS = $$QMAKE_CFLAGS
QMAKE_CXXFLAGS_DEPS = $$QMAKE_CFLAGS_DEPS
QMAKE_CXXFLAGS_WARN_ON = $$QMAKE_CFLAGS_WARN_ON
QMAKE_CXXFLAGS_WARN_OFF = $$QMAKE_CFLAGS_WARN_OFF
QMAKE_CXXFLAGS_RELEASE = $$QMAKE_CFLAGS_RELEASE
QMAKE_CXXFLAGS_DEBUG = $$QMAKE_CFLAGS_DEBUG
QMAKE_CXXFLAGS_YACC = $$QMAKE_CFLAGS_YACC
QMAKE_CXXFLAGS_THREAD = $$QMAKE_CFLAGS_THREAD
QMAKE_CXXFLAGS_RTTI_ON = -frtti
QMAKE_CXXFLAGS_RTTI_OFF = -fno-rtti
QMAKE_CXXFLAGS_EXCEPTIONS_ON = -fexceptions -mthreads
QMAKE_CXXFLAGS_EXCEPTIONS_OFF = -fno-exceptions

The way QT deals with compiler options is through the .pro file. It is a double edged sword if I may. It creates a nice abstraction, especially when compiling large projects. The problem is that you have to either look up or memorize how to add the flag. In the case of C++0X, you have to add the following flag to your .pro file:
QMAKE_CXXFLAGS += -std=c++0x
Fortunately most of the flags that you need are automatically added if you use QtCreator.

Related

Unable to add below statements in code QMAKE_CXXFLAGS += -std=c++14

I am using chrono library for calculating elapsed time but I am getting below warning.
warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
I am beginner in C++ and don't know how to add
QMAKE_CXXFLAGS += -std=c++14
Any help would be highly appreciated.
IF you are using a qmake project, it should be
CONFIG += c++14
in the project file as mention in an answer to this post: How can I use C++14 features when building qmake projects
You should add the flags in your compiling command:
g++ -std=c++14 chronoprog.cpp -lmpir -o chronoprog

Qt debugging in release mode - all methods not working

I am trying to build my Qt app with MinGW in QtCreator in order to try and debug a release-only crash.
Here is what I put in my .pro file (also tried putting it directly into qmake.conf):
QMAKE_CFLAGS_RELEASE += -g
QMAKE_CXXFLAGS_RELEASE += -g
QMAKE_CXXFLAGS += -g
as well as tried with
QMAKE_CFLAGS_RELEASE += -ggdb
QMAKE_CXXFLAGS_RELEASE += -ggdb
QMAKE_CXXFLAGS += -ggdb
I tried them separately or together like this:
QMAKE_CFLAGS_RELEASE += -g
QMAKE_CXXFLAGS_RELEASE += -g
QMAKE_CXXFLAGS += -g
QMAKE_CFLAGS_RELEASE += -ggdb
QMAKE_CXXFLAGS_RELEASE += -ggdb
QMAKE_CXXFLAGS += -ggdb
I have also manually edited the generated Makefile(Makefile.Release to be exact and checked Makefile.Debug just in case) to remove the NO_DEBUG flag (I don't know why it was being put there) and to make sure the -g and -ggdb flags are there.
The result of all of this is that the app took almost twice as long to build, however it was exactly the same size as before and didn't have any debugging symbols and trying to debug it didn't succeed.
What else can I do? I know it's possible, but looking for other answers only directed me to using those flags and that didn't help at all(only made build slower)
In qmake.conf (can't recall where this is now) the default behaviour is set as:
QMAKE_LFLAGS_RELEASE = -Wl,-s
Where the -s means the linker does away with the symbolic info. So in addition to the flags you have set you need to clear the flags (or overwrite) in QMAKE_LFLAGS_RELEASE. In your case just setting it to empty should do it, so in your .pro file add:
QMAKE_LFLAGS_RELEASE =
FURTHER NOTES
If you add the line message(QMAKESPEC: $$QMAKESPEC) to your .pro file, when you run qmake you will see the path for your make spec. Look in the qmake.conf file (it will have includes) and work your way through them.
Assuming you are using windows (because I did not see the -Wl,-s option set in the linux make specs for Qt 5.5.1) then the file in question is:
<path-to-qt>/5.5/gcc_64/mkspecs/win32-g++/qmake.conf
Anyway, I did not see any other useful flags set (I am using linux so this was only checked by eye. However if you are worried, then add the following to your .pro file:
message(QMAKE_LFLAGS_RELEASE: $$QMAKE_LFLAGS_RELEASE)
To check it before you make changes and then you will have the complete picture of what is going on :)
I've just figured it out, and only saw #code_fodder's answer after it. It looks like #code_fodder is essentially right, but I'm adding more details to reflect the precise steps that helped me.
I've specified qmake arguments in the Qt Creator project settings like this:
QMAKE_CXXFLAGS+=-g QMAKE_LFLAGS_RELEASE-=-Wl,-s
This essentially adds -g to the compiler flags and removes -Wl,-s from the linker flags (which strips all symbols). Then I ran qmake and did full rebuild. After that, debugging sort of worked—as you'd expect for a release build: jumping from line to line in a somewhat random fashion because of the optimizations, but at least it worked.
I'm using 32-bit Qt 5.5.1 with bundled MinGW on a 64-bit Windows 7 system, if that is of any help.

How to enable C++11 in Qt Creator?

The title is pretty self-descriptive. I've downloaded Qt Creator 2.7.0, and I am trying to compile some basic C++11 code:
int my_array[5] = {1, 2, 3, 4, 5};
for(int &x : my_array)
{
x *= 2;
}
I'm receiving the following error:
range based for loops are not allowed in c++ 98 mode
Yet, according to this article this version of Qt Creator supports C++11. So how do I enable it?
According to this site add
CONFIG += c++11
to your .pro file (see at the bottom of that web page). It requires Qt 5.
The other answers, suggesting
QMAKE_CXXFLAGS += -std=c++11 (or QMAKE_CXXFLAGS += -std=c++0x)
also work with Qt 4.8 and gcc / clang.
Add this to your .pro file
QMAKE_CXXFLAGS += -std=c++11
or
CONFIG += c++11
As an alternative for handling both cases addressed in Ali's excellent answer, I usually add
# With C++11 support
greaterThan(QT_MAJOR_VERSION, 4){
CONFIG += c++11
} else {
QMAKE_CXXFLAGS += -std=c++0x
}
to my project files. This can be handy when you don't really care much about which Qt version is people using in your team, but you want them to have C++11 enabled in any case.
add to your qmake file
QMAKE_CXXFLAGS+= -std=c++11
QMAKE_LFLAGS += -std=c++11
If you are using an earlier version of QT (<5) try this
QMAKE_CXXFLAGS += -std=c++0x
The only place I have successfully make it work is by searching in:
...\Qt\{5.9; or your version}\mingw{53_32; or your
version}\mkspecs\win32-g++\qmake.conf:
Then at the line:
QMAKE_CFLAGS += -fno-keep-inline-dllexport
Edit :
QMAKE_CFLAGS += -fno-keep-inline-dllexport -std=c++11

Qt 5.0 and c++11 with OSX Mountain Lion

I'm giving c++ a try again after being away for 7 years. I've downloaded the newly released Qt 5.0 sdk for osx, but I can't get a basic c++11 feature compiled using Qt Creator. The following statement:
auto i = 3;
results in a compilation error:
mainwindow.cpp:19: error: ISO C++ forbids declaration of 'i' with no type
I've google around for similar problems and found suggestions to put
QMAKE_CXXFLAGS += -std=c++11
or
CONFIG += c++11
in the .pro file. Unfortunately without success .The build fails on unrecognized command line options.
I must be doing something wrong. Any suggestions?
Thanks,
Frans
It's looks like Qt Creator for Mac bug.
I fixed it using
QMAKE_CXXFLAGS += -std=c++11 -stdlib=libc++ -mmacosx-version-min=10.7
LIBS += -stdlib=libc++ -mmacosx-version-min=10.7
in .pro file
I don't know if this is still an issue for you.
On my current system (Max OS X Yosemite - Qt 5.4 - QtCreator 3.0 - XCode 6.1.1) this works like a charm:
In .pro file:
macx {
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.7
QMAKE_CXXFLAGS += -mmacosx-version-min=10.7
QMAKE_CFLAGS += -mmacosx-version-min=10.7
QMAKE_MAC_SDK = macosx10.9
}
macx {
QMAKE_CXXFLAGS += -std=c++11 -stdlib=libc++
QMAKE_CFLAGS += -std=c++11 -stdlib=libc++
}
CONFIG += c++11
Make sure you have the latest Mac OS X SDK installed too.
According to the gcc feature list, auto is supported since gcc 4.4through:
g++ -std=c++0x
you could also try the gcc-specific form:
g++ -std=gnu++0x
Maybe, in your case, an additional problem occurs (g++ parameters not correctly promoted to the compiler).

CXXFLAGS modification of Qt pro file? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Configuring the GCC compiler switches in Qt, QtCreator, and QMake
I would like to use -O1 instead of -O2 in my makefile (CFLAGS and CXXFLAGS) for my Linux build. My understanding of how these makefiles are generated based on the .pro file is somewhat lacking. This is because the version of Qt combined with the version of G++ I am using has instabilities when -O2 is present.
Presently, I am running a replacement script, after I run qmake, which does this:
sed -i 's/\-O2/\-O1/g' AllProjects/Makefile.Release
This is a ghetto solution. A much better solution would be to modify the .pro file somehow to pass along these directives. I am not sure how CFLAGS and CXXFLAGS are being generated though.
I have tried passing a
linux-g++-{
CFLAGS += -O1
CXXFLAGS += -O1
CONFIG += -O1
}
which did not work.
You were very close. What you want is:
QMAKE_CXXFLAGS += -O1
If you would like to apply flags to just the release build, then you can use this:
QMAKE_CXXFLAGS_RELEASE += -O1
You also probably want to change your condition to be a little more flexible. In summary, something like this:
*-g++* {
QMAKE_CXXFLAGS += -O1
}
More in the documentation here: http://qt-project.org/doc/qt-5.0/qtdoc/qmake-variable-reference.html#qmake-cxxflags