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

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

Related

Can't use c++17 features using g++ 7.2 in QtCreator

I have recently updated gcc and g++ to version 7.2. I would like to try out std::experimental::any and std::variant in particular, and I am using Qt 5.9.1 in QtCreator.
So far I have written this in the project file:
CONFIG += c++17
And I have added the correct headers in the correct places:
#include <variant>
#include <experimental/any>
Any works fine, no problems there. However, when I include the variant header file, I get this error:
/usr/include/c++/7/bits/c++17_warning.h:32: error: #error This file requires compiler and library support for the ISO C++ 2017 standard. This support must be enabled with the -std=c++17 or -std=gnu++17 compiler options.
#error This file requires compiler and library support \
^~~~~
I have tried a variety of things in the project file, here is the full list:
CONFIG += c++17
&
CONFIG += c++1z
&
QMAKE_CXXFLAGS += -std=c++17
&
QMAKE_CXXFLAGS += -std=c++1z
&
CONFIG += c++17
QMAKE_CXXFLAGS += -std=c++17
&
CONFIG += c++1z
QMAKE_CXXFLAGS += -std=c++1z
&
CONFIG += c++11
CONFIG += c++14
CONFIG += c++17
That's every stab in the dark I could think of. What am I missing? And why does experimental::any compile when variant doesn't?
I know I shouldn't use CONFIG += c++xx and QMAKE_CXXFLAGS together in this way, but I thought I'd give it a go as nothing else works. For bonus points, I'm also wondering, should I add the CONFIG calls for 14 and 11 when I already CONFIG for 17?
EDIT:
Here is the compiler output with most of my filenames scrubbed out:
18:04:10: Running steps for project AIQt...
18:04:10: Configuration unchanged, skipping qmake step.
18:04:10: Starting: "/usr/bin/make"
/home/pete/Qt/5.9.1/gcc_64/bin/qmake -o Makefile ../AIQt/AIQt.pro -spec linux-g++ CONFIG+=debug CONFIG+=qml_debug
WARNING: Failure to find: ../src/stdafx.h
WARNING: Failure to find: ../src/Csound/csd.h
g++ -c -pipe -g -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_DATAVISUALIZATION_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../AIQt -I. -I../src -I../src/AIBase -I../src/Maths -I../src/Random -isystem /usr/local/include/csound -I../../../../Qt/5.9.1/gcc_64/include -I../../../../Qt/5.9.1/gcc_64/include/QtDataVisualization -I../../../../Qt/5.9.1/gcc_64/include/QtWidgets -I../../../../Qt/5.9.1/gcc_64/include/QtGui -I../../../../Qt/5.9.1/gcc_64/include/QtCore -I. -isystem /usr/include/libdrm -I. -I../../../../Qt/5.9.1/gcc_64/mkspecs/linux-g++ -o main.o ../AIQt/main.cpp
In file included from /usr/include/c++/7/variant:35:0,
from ..###,
from ..###,
from ..###,
from ..###,
from ..###,
from ..###,
from ..###,
from ..###:
/usr/include/c++/7/bits/c++17_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2017 standard. This support must be enabled with the -std=c++17 or -std=gnu++17 compiler options.
#error This file requires compiler and library support \
^~~~~
In file included from ..###,
from ..###
from ..###,
from ..###,
from ..###,
from ..###,
from ..###:
../src/AIBase/Geno.h:70:18: error: ‘variant’ in namespace ‘std’ does not name a type
std::variant m_valueVariant;
^~~~~~~
In file included from ..###,
from ..###,
from ..###,
from ..###,
from ..###,
from ..###:
../src/AIBase/Pheno.h:22:13: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
const double getGenoValue(size_t genoIndex) const;
^~~~~
../src/AIBase/Pheno.h:24:13: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
const UserRating getRating() const;
^~~~~
In file included from ..###,
from ..###:
../AIRadioQt/GraphDialog.h:16:15: warning: declaration ‘struct ar::ai::ClusterList’ does not declare anything
class ar::ai::ClusterList;
^~~~~~~~~~~
make: *** [main.o] Error 1
18:04:13: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project AIQt (kit: Qt 5.9.1 GCC 64bit)
The kit Qt 5.9.1 GCC 64bit has configuration issues which might be the root cause for this problem.
When executing step "Make"
18:04:13: Elapsed time: 00:03.
ANSWER:
As mentioned by nwp, I just had to clean it and rebuild.
Another poster also commented that CONFIG += c++17 doesn't appear to be supported yet, so it is necessary to use QMAKE_CXXFLAGS += -std=c++17. He quickly deleted his comment though, so I am unable to thank him personally for going to the effort of checking the docs for me.
CONFIG += c++17 can be used with Qt 5.12 and later.
For Qt 5.11 and earlier, it is not a recognized QMake flag and you have to get your hands a bit dirty.
Adding QMAKE_CXXFLAGS += -std=c++17 does the job for GCC & Clang; for MSVC you will probably need to specify /std:c++17 or /std:c++latest.
Edit 3/2019: You can use CONFIG += c++17 since Qt 5.12.
The actual flag is c++1z, not c++17. In short, to get C++17 support, you don't need to modify QMAKE_CXXFLAGS and can instead simply use CONFIG += c++1z.
Discussion on the reason why can be found in this bug report, but it pretty much amounts to "we implemented it as c++1z before C++17 was standardized, and now we won't bother aliasing it."
Note: I realize you just needed a clean and rebuild. I'm answering the underlying question of "what flags do I need to use to enable C++17 support?"
By Example C++17 C++20 Builds with Qt5 version 5.15.3 (and above) on Linux (or similar on Windows)
Summary:
Inside CMakeLists.txt file for an application written in C++17 and using Qt
cmake_minimum_required(VERSION 3.16.0)
project(helloworld VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
A qt project configuration eg mygame.pro may have c++1z for C++17 or c++2a for C++20 or c++11 for older projects:
CONFIG += c++1z
On the command line of configure eg a C++20 project config, always use a shadow build:
../configure -c++std c++2a
cmake --build .. --parallel
Windows or Linux compiler command arguments examples with or without qt; at time of writing C++20 is c++2a and C++17 is c++1z others to follow.
$ g++ -std=c++2a hello_linux_is_the_worlds_device_future.cpp
c:\hello> cl /std=c++1z /EHsc hello_windows_is_in_decline.cpp
On windows notice the forward /slash.
More Detail:
I have found that building a new C++ project or even building ALL of qt5 open source thus creating my own qt release (which is tough) is possible by C++17 or C++20 (Qt6 requires at least C++17). I cloned and then checked out version qt5 5.15 i.e. ALL of qt5 open source and built it using C++20 - the build takes 23GB ... you don't need to do all that.
Note the config examples below, ... 4 hours of build on a fast PC are super fussy and I managed eventually. Anyway the point is that Qt5.15 was happy to build itself with the C++17 or C++20 settings (in my case using gcc g++-8 or g++-9) by example:
Just take a look at this cmake Get Started (qt6 C++17 example) but 20 works just as well)
Qt5 open source code total build of qt5.15.3 with C++20 configure example:-
mkdir qt5-build
cd qt5-build
../configure -release -opensource -nomake examples -nomake tests
-skip qtdocgallery -c++std c++2a -confirm-license -opengl desktop
-platform linux-g++-64 # put this all on one-line please.
cmake --build .. --parallel
OR use make -j4 as I did.
Considerable care was taken to write this for my own notes which I share, I am happy to make corrections myself and suggestions are welcome; never ever hack into an authors work and change it! Just as I do not go into a public library and remove pages from books. Examples count more than non-solutions, however examples age faster!
For Windows:
I'm download qt-opensource-windows-x86-5.11.1, icncluded MinGW32.
For MinGW64 I'm download qt-5.5.0-x64-mingw510r0-seh-rev0 and install only compiler.
Configure QtCreator, as says here.
Create new project and add QMAKE_CXXFLAGS += -std=gnu++1z to .pro file (gcc doc).
For test, try compile this simple code:
#include <optional>
std::optional<int> foo()
{
return std::nullopt;
}
int main(int argc, char *argv[])
{
foo();
}

Error: ‘to_string’ is not a member of ‘std’ when compiling in command line with gcc 4.8.2, works in NetBeans?

I am doing tests for my code with GoogleTest (1.7.0). I must have the tests compiling in both NetBeans (8.0.2) and straight from command line.
NetBeans has GoogleTest integrated as suggested here: https://youtu.be/TS2CTf11k1U.
I have also build GoogleTest from the command line (in different place) with the instructions provided with the package.
The problematic line of code is (a and b are doubles):
std::string input = std::to_string(a) + " " + std::to_string(b);
which gives error: ‘to_string’ is not a member of ‘std’, when compiling with
g++ -isystem -std=c++0x ~/gtest-1.7.0/include -pthread ~/Task1/test_source.cpp libgtest.a -o test_module1_1
as instructed in GoogleTest documentation.
The strange thing here is that when using the "Test" from NetBeans (as seen on video), the tests compile and run correctly.
I am using Ubuntu 14.04 and I have already tried to update g++ (not to a higher version though), and I have also tried specifying the version with -std=c++11 instead of -std=c++0x.
In NetBeans there is this warning visible:
Library File /usr/include/c++/4.8/string
but there is an unresolved #include <stddef.h>
in included /usr/include/_G_config.h
I found out that this might be because multilib and I have also tried to fix that by checking that it is installed. And this doesn't really cause any warnings during compile so it might be just that the IDE is confused.
But conserning the real problem about compile errors:
Is there something wrong with the GoogleTest framework, the compiler or my code?
PS. Preferably don't suggest that I just don't use to_string as this system must be usable by students studying C++11 and really it shouldn't be that they can't use all the functionality that should be supported.
I think your problem is the order in which you're passing command line arguments to gcc
g++ -isystem -std=c++0x ~/gtest-1.7.0/include ...
^^^^^^^^^^^^^^^^^^^
The -isystem flag should be followed by a path that contains headers which you want gcc to treat as system headers. So the -std=c++0x flag is being incorrectly consumed by the -isystem flag. You probably want the command line to be
g++ -std=c++0x -isystem ~/gtest-1.7.0/include ...

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

Where in Qt Creator do I pass arguments to a compiler?

Where in Qt Creator do I pass arguments to a compiler?
It isn't really that obvious.
Depending on your build system it's either in your qmake project file(.pro, standard for new projects) or in one of the CMake files (CMakeLists.txt, used by KDE and several other projects).
Using .pro:
QMAKE_CXXFLAGS += -O2
Using CMake:
set( CMAKE_CXX_FLAGS "-g -Wall")
To add compiler flags, open your .pro file and add a line like this:
QMAKE_CXXFLAGS += -std=c++0x
For standard flags like debug vs. release etc. you should try to use the predefined qmake options (see QMake documentation) for the sake of platform and compiler-independency, as QMake will map them to the compiler-specific flags.
If your intention is to precompile some source code you can do like this:
/A/ In your .pro file you can add a line like this:
DEFINES += HOPLA
/B/ In you .cpp or .h file you can use it like this
#ifdef HOPLA
// Do something
#else
// Do something different
#endif
for C projects, add the following line in .pro file
QMAKE_CFLAGS += -std=c99
in the .pro file you can add variables which modify the make behavior for example, if you try to execute the following command:
g++ -Wall -I/usr/include/cppconn -o exe main.cpp -L/usr/lib -lmysqlcppconn
you must add the following lines in the .pro file
INCLUDEPATH += /usr/include/cppconn
LIBS += -L/usr/lib -lmysqlcppconn
Check the image below.
For more information on the available variables that QT IDE uses, you can visit the following link where they explain in more detail each one. Qt Documentation: Variables
As the elected answer points, for CMake based projects, you can edit the CMakeLists.txt and set the flags for the compiler, and for this case, I have a pictorial demonstration on how to add flags on QtCreator/CMake.
I wanted to add the '-pedantic' flag, which warns about extensions, without throwing errors while executing the program, and here's pictorial example of how to enable compiler flags on CMake while using QtCreator:
For more context:
On the example below, I'm setting the size of a Static Array at Runtime, something that is only possible with Variable-length Array feature, which is available at C99, but defined as optional feature starting from C11. Without -pedantic flag being available for the compiler, the warning would't be displayed after compiling the code.

`#include <iostream>` with `-std=c++0x` is broken

If I specify -std=c++0x to g++, then I can't #include <iostream>. I get the following error messages (g++ 4.4.0 under mingw):
In file included from c:\qt\2010.05\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/postypes.h:42,
from c:\qt\2010.05\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/iosfwd:42,
from c:\qt\2010.05\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/ios:39,
from c:\qt\2010.05\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/ostream:40,
from c:\qt\2010.05\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/iostream:40,
from f.cpp:1:
c:\qt\2010.05\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/cwchar:159: error: '::swprintf' has not been declared
c:\qt\2010.05\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/cwchar:166: error: '::vswprintf' has not been declared
Any ideas why? Has this been fixed in the latest g++? (And if so, does anybody know how to incorporate the latest g++ into Qt?)
It seems to be a bug. There is a thread with a simple patch (in the very end).
if win32{
QMAKE_CXXFLAGS += -std=gnu++0x
}
else {
QMAKE_CXXFLAGS += -std=c++0x
}