How to enable C++11 in Qt Creator? - c++

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

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

LLVM Error : External function could not be resolved

I am reading the LLVM's Kaleidoscope tutorial (http://llvm.org/docs/tutorial/index.html).
I wanted to compile and test the language. After some compiler's errors (EngineBuilder and Module's constructor, linking libs...), the example program was built. Then, I tried the language. I got a few problems with InitializeNativeTargets, DataLayoutPass... But I managed to correct them.
Howewer, I don't manage to resolve one error. When I write extern printd(x); printd(5);, the program doesn't work : "LLVM ERROR : Program used external function 'printd' which could not be resolved".
I looked for the solution on the net. I read a lot of webpages, but nothing worked.
How can I resolve this problem ? Why LLVM can't find the external function 'printd', which is included in the program ? Thanks in advance for your answer.
The used code : https://docs.google.com/document/d/1Qb-zUGaUUIF354uFCXv1iuq8n_rjya6IHDW4WCPWN_4/edit?usp=sharing
The .pro file (Qt Creator) :
QT += core
QT -= gui
TARGET = Kaleidoscope
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /home/alainetflo/Documents/me/C++/LLVM/llvm-3.5.0.src/include
DEPENDPATH += /home/alainetflo/Documents/me/C++/LLVM/llvm-3.5.0.src/include
LIBS += `llvm-config --ldflags --libs all --system-libs` -Wl,-no-as-needed
QMAKE_CFLAGS += -m32
QMAKE_CXXFLAGS += -rdynamic -std=c++11 -O3
I use GCC 4.8.2, LLVM 3.5 and Qt 5.3.1 on Ubuntu 14.04 32bits.
Finally, I found a great link on the net : http://koichitamura.blogspot.fr/2011/01/since-i-went-to-held-several-weeks-ago.html.
The error came from the wrong place of the -rdynamic argument in the .pro file (-rdynamic must be after the linking options). You need this argument because (GCC man page) :
This instructs the linker to add all symbols, not only used ones, to the dynamic symbol table.
I changed the .pro file :
QT += core
QT -= gui
TARGET = Test01
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /home/alainetflo/Documents/me/C++/LLVM/llvm-3.5.0.src/include
DEPENDPATH += /home/alainetflo/Documents/me/C++/LLVM/llvm-3.5.0.src/include
INCLUDEPATH += /usr/include/i386-linux-gnu/c++/4.8 \
/usr/include/c++/4.8
LIBS += `llvm-config --ldflags --libs all --system-libs` -Wl,-no-as-needed -rdynamic
QMAKE_CFLAGS += -m32
QMAKE_CXXFLAGS += -g -std=c++11 -O3
In this way, the program works with no error ! (The tutorial's example runs (http://llvm.org/docs/tutorial/LangImpl6.html#kicking-the-tires))

Using libc++ instead of libstdc++ in Qt Creator

I have clang++ 3.5, g++ 4.9.1, libc++ and Qt Creator installed on Ubuntu 14.04.1.
I usually use clang++ as compiler. But recently I found that libstdc++ from g++ used as C++ Standard Library. As I know the latter not fully supports C++14 innovations at the moment.
How to replace libstd++ with libc++ when project compiled with clang++?
What I already done (.pro-file):
QMAKE_CXXFLAGS_CXX11 = -std=gnu++1y
CONFIG *= c++11
QMAKE_CXXFLAGS += -stdlib=libc++
LIBS += -stdlib=libc++
But the Qt Creator editor still uses libstdc++ as DEPENDPATH when crawling through included files. How to fix such ill behaviour? Maybe should I fix something in mkspec files?
The standard library is specified in the mkspec files.
In linux, you can use : qmake -spec linux-clang-libc++
I think the equivalent in qtcreator is to define a new "kit" and specify "linux-clang-libc++" in the field "Qt mkspec"

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).

Adding extra compiler option in Qt [duplicate]

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.