Tell QMake -std=C++17. QMAKE_CXXFLAGS overridden - c++

I use Qt 5.9.1 and all works fine. Except of one little nuisance: it overrides my -std=C++17.
The project:
#CONFIG += c++14 #makes -std=gnu++1y
#CONFIG += c++17 # is not supported by Qt591
QMAKE_CXXFLAGS += -std=c++17
TARGET = equipment
TEMPLATE = app
SOURCES += ....
....
Compile instructions:
g++ -c -pipe -std=c++17 -g -std=gnu++11 -Wall -W -D_REENTRANT -fPIC
-DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_SERIALPORT_LIB -DQT_CORE_LIB
-I../app -I/opt/Qt5.9.1/5.9.1/gcc_64/include -I/opt/Qt5.9.1/5.9.1/gcc_64
/include/QtCore -I. -I/opt/Qt5.9.1/5.9.1/gcc_64/mkspecs/linux-g++
-o test0.o ../test0.cc
We can see two -std= and the second -std=gnu++11 by QMake overrides mine first -std=c++17.
How can I tell QMake to use -std=C++17?

Try CONFIG += c++1z and if it doesn't work pass it to compiler with QMAKE_CXXFLAG.

Related

How to set qmake to C++14 with recent MinGW?

I know there are many duplicates.
This is my Test.pro:
CONFIG += c++14
SOURCES += main.cpp
and my main.cpp:
int main(){}
According to the many duplicates this should give me C++14. However, when I build the project with Qt Creator 4.2.0 with Qt 5.8.0-1 and MinGW gcc 5.3.0-1 installed via the maintenance tool I get
g++ -c -pipe -fno-keep-inline-dllexport -g -std=gnu++1y -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I..\Test -I. -IC:\Qt\5.8\mingw53_32\include -IC:\Qt\5.8\mingw53_32\include\QtGui -IC:\Qt\5.8\mingw53_32\include\QtANGLE -IC:\Qt\5.8\mingw53_32\include\QtCore -Idebug -IC:\Qt\5.8\mingw53_32\mkspecs\win32-g++ -o debug\main.o ..\Test\main.cpp
which is not the -std=c++14 I expect.
I tried all kinds of tricks from other questions such as
QMAKE_CXXFLAGS_CXX14 = -std=c++14
CONFIG += c++14
QMAKE_CXXFLAGS += -std=c++14
SOURCES += main.cpp
which results in
g++ -c -pipe -fno-keep-inline-dllexport -std=c++14 -g -std=gnu++1y -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I..\Test -I. -IC:\Qt\5.8\mingw53_32\include -IC:\Qt\5.8\mingw53_32\include\QtGui -IC:\Qt\5.8\mingw53_32\include\QtANGLE -IC:\Qt\5.8\mingw53_32\include\QtCore -Idebug -IC:\Qt\5.8\mingw53_32\mkspecs\win32-g++ -o debug\main.o ..\Test\main.cpp
where the second option overwrites the first, meaning it is still in gnu++1y-mode or just
QMAKE_CXXFLAGS += -std=c++14
SOURCES += main.cpp
which also results in
g++ -c -pipe -fno-keep-inline-dllexport -std=c++14 -g -std=gnu++11 -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I..\Test -I. -IC:\Qt\5.8\mingw53_32\include -IC:\Qt\5.8\mingw53_32\include\QtGui -IC:\Qt\5.8\mingw53_32\include\QtANGLE -IC:\Qt\5.8\mingw53_32\include\QtCore -Idebug -IC:\Qt\5.8\mingw53_32\mkspecs\win32-g++ -o debug\main.o ..\Test\main.cpp
I deleted the build directory and the Test.pro.user file to force a build from scratch, nothing gave me C++14.
How do I tell qmake to use C++14?
The version of Qt that you're using doesn't explicitly support the compiler you're using. You can do either one of the following:
Set both QMAKE_CXXFLAGS_CXX14 and QMAKE_CXXFLAGS_GNUCXX14 in your project:
win32-g++ {
QMAKE_CXXFLAGS_CXX14 = -std=c++14
QMAKE_CXXFLAGS_GNUCXX14 = -std=c++14
}
Edit the default values of those two variables as above, in mkspecs/win32-g++/qmake.conf within your Qt installation folder.
Add a new mkspec copied from win32-g++, targeting your compiler, and build your Qt using it. All the project that use that Qt will then behave correctly w.r.t. C++14 support.

qmake with Jenkins

I am trying to make Jenkins do the compile and build for me whenever I push commit to Git. I have a helloworld app created in Qt Creator. It does compile and build in Qt Creator, it does show on my phone, but it doesn't want to compile and build with Jenkins.
I am using the same shell commands which QtC used:
qmake helloworld.pro -r -spec android-g++;
make;
The console output is:
+ qmake helloworld.pro -r -spec android-g++
+ make
/home/ndk/toolchains/arm-linux-androideabi-4.9-/prebuilt/bin/arm-linux-androideabi-g++ -c -pipe -std=c++11 -O2 -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -mthumb -Wall -Wno-psabi -W -I/usr/share/qt4/mkspecs/android-g++ -I. -I. -I/home/ndk/sources/cxx-stl/gnu-libstdc++//include -I/home/ndk/sources/cxx-stl/gnu-libstdc++//libs//include -I/home/ndk/platforms//arch-arm//usr/include -o main.o main.cpp
In file included from main.cpp:1:0:
mainwindow.h:4:23: fatal error: QMainWindow: No such file or directory
#include <QMainWindow>
^
compilation terminated.
Makefile:187: recipe for target 'main.o' failed
make: *** [main.o] Error 1
My helloworld.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = helloworld
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
CONFIG += mobility
MOBILITY =
main.cpp
#include "mainwindow.h"
#include <QMainWindow>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
EDIT/UPDATE:
So far, I have tried to specify the full path to qmake, to be sure that I use Qt's qmake.
/home/qt/5.7/android_armv7/bin/qmake helloworld.pro -r -spec android-g++;
make;
The output is:
/home/qt/5.7/android_armv7/bin/uic mainwindow.ui -o ui_mainwindow.h
/home/ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ -c -Wno-psabi -march=armv7-a -mfloat-abi=softfp -mfpu=vfp -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums -DANDROID -Wa,--noexecstack -fno-builtin-memmove -std=c++11 -O2 -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -mthumb -Wall -Wno-psabi -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I../../qt/5.7/android_armv7/include -I../../qt/5.7/android_armv7/include/QtWidgets -I../../qt/5.7/android_armv7/include/QtGui -I../../qt/5.7/android_armv7/include/QtCore -I. -I. -I../../ndk/sources/cxx-stl/gnu-libstdc++/4.9/include -I../../ndk/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include -I../../ndk/platforms/android-9/arch-arm/usr/include -I../../qt/5.7/android_armv7/mkspecs/android-g++ -o main.o main.cpp
/home/ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ -c -Wno-psabi -march=armv7-a -mfloat-abi=softfp -mfpu=vfp -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums -DANDROID -Wa,--noexecstack -fno-builtin-memmove -std=c++11 -O2 -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -mthumb -Wall -Wno-psabi -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -I../../qt/5.7/android_armv7/include -I../../qt/5.7/android_armv7/include/QtWidgets -I../../qt/5.7/android_armv7/include/QtGui -I../../qt/5.7/android_armv7/include/QtCore -I. -I. -I../../ndk/sources/cxx-stl/gnu-libstdc++/4.9/include -I../../ndk/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include -I../../ndk/platforms/android-9/arch-arm/usr/include -I../../qt/5.7/android_armv7/mkspecs/android-g++ -o mainwindow.o mainwindow.cpp
mainwindow.cpp:3:25: fatal error: QtGui/QAction: No such file or directory
#include <QtGui/QAction>
^
compilation terminated.
Makefile:1780: recipe for target 'mainwindow.o' failed
make: *** [mainwindow.o] Error 1
Most likely your jenkins setup uses the wrong qmake. There's a 1:1 mapping between a Qt installation and its qmake. So, your install of Qt for android will have its own qmake that you must refer to by full path! Qt Creator sets up the path so that this happens automatically, Jenkins doesn't - and it shouldn't anyway.
When building for any particular Qt version, you only need to invoke its qmake explicitly. From then onwards, as long as the proper compiler is in the path, things will happen as they should and you don't ever need to refer to that Qt version manually again. The makefiles will all refer to Qt libraries, headers and tools from that Qt install.
You also don't need the -spec argument. Each Qt installation is compiled for a particular mkspec, so qmake itself knows exactly what the spec is. I have no idea why QtCreator gives that argument, it doesn't make much sense.
The only Qt header path that's in your commandline is the one for the mkspec. I would blame the server's Qt installation for this.
Ensure Qt for Android is properly installed on the server, and that it is its qmake you are calling.

LDFLAGS in Qt pro file

I have a Qt project that I'm compiling with GCC and MinGW for Ubuntu and Windows.
I got a requirement to harden it by adding the following LDFLAGS:
Stack execution protection: LDFLAGS="-z noexecstack"
Data relocation and protection (RELRO): LDLFAGS="-z relro -z now"
The question is can this be done with .pro file and how? I found it easy to add LFLAGS and CFLAGS in the project file but couldn't find anything for LDFLAGS. Even the output Makefiles don't seem to have any LDFLAGS defined.
One way I found after long googling was to add QMAKE_CFLAGS_RELEASE += "--noexecstack" in the .pro file but I'm not convinced this is the right way.
After the line above, the generated Makefile looks like this:
CC = gcc
CXX = g++
DEFINES = -DUNICODE -DMY_LIBRARY -DQT_NO_DEBUG -DQT_NETWORK_LIB -DQT_CORE_LIB
CFLAGS = -pipe -fno-keep-inline-dllexport -O2 -fstack-protector-strong -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security --noexecstack -Wall -Wextra $(DEFINES)
CXXFLAGS = -pipe -fno-keep-inline-dllexport -O2 -frtti -Wall -Wextra -fexceptions -mthreads $(DEFINES)
LINKER = g++
LFLAGS = -Wl,-s -shared -Wl,-subsystem,windows -mthreads -Wl,--out-implib,C:\libmylib0.a
noexecstack appears in the CLFAGS list but not sure if that's alright. CFLAGS is not the same as LDFLAGS. It doesn't seems to validate the command either since --thisdoesntexist seemed to go through as well when I tried.
Thank you in advance.
EDIT:
Based on Gwen's answer I tried adding QMAKE_LFLAGS += "-z noexecstack -z relro -z now" but this produced an error from ld.exe:
error: unrecognized option '-z'
EDIT2:
Tool versions:
C:\Qt\Qt5.5.0\Tools\mingw492_32\bin>ld.exe -v
GNU ld (GNU Binutils) 2.24
C:\Qt\Qt5.5.0\Tools\mingw492_32\bin>g++.exe --version
g++.exe (i686-posix-dwarf-rev1, Built by MinGW-W64 project) 4.9.2
With my configuration (QtCreator + Visual C++ compiler), the LFLAGS defined in the makefile is given to the linker, contrary to what is stated in the GNU make documentation:
LDFLAGS
: Extra flags to give to compilers when they are supposed to invoke the linker[...]
LFLAGS
: Extra flags to give to Lex.
I think you should try adding QMAKE_LFLAGS += "-z noexecstack -z relro -z now" to your .pro file, empty your build folder, re-run qmake, and see if the option is given to the linker.

Remove flags (-fpermissive) from Makefile (Qt)

I have Googled everywhere, and have not managed to figure out how to remove the -fpermissive flag from my Qt-generated Makefiles. The -fpermissive flag is defaultly there, even if I do QMAKE_CXXFLAGS -= -fpermissive in my Qt project file, attempting to remove it. Here is my Makefile where the flags are:
CFLAGS = -fpermissive -finline-functions -Wno-long-long -g -Wall $(DEFINES)
CXXFLAGS = -fpermissive -finline-functions -Wno-long-long -g -fexceptions -mthreads -frtti -Wall $(DEFINES)
How can I remove these?
My QtCreator-generated makefiles don't have -fpermissive anywhere in them, at least as far as I can Ctrl+F.
Check your project's Build Settings, and make sure you don't have -fpermissive in there as command-line arguments.
This is the the same two lines in my QMake-generated makefile:
CFLAGS = -pipe -fno-keep-inline-dllexport -O2 -Wall -Wextra $(DEFINES)
CXXFLAGS = -pipe -fno-keep-inline-dllexport -O2 -std=c++11 -frtti -Wall -Wextra -fexceptions -mthreads $(DEFINES)
My makefiles also references a file called 'qmake.conf', which for me is located at:
C:/Qt/Qt5.0.1/5.0.1/mingw47_32/mkspecs/win32-g++/qmake.conf
It looks like the qmake.conf is used to help generate makefiles for different compilers and platforms, and can add arguments to your makefile.

Qt compiler flags order

My goal is to get rid of some types of compiler warnings. I found out that I can do that by adding compiler flags in my .pro file:
QMAKE_CXXFLAGS += -Wno-unused-variable -Wno-reorder
The problem is that they are added before flags that are generated by Qt build system. I've examined my compiler output:
g++-4.2 -c -pipe -Wno-unused-variable -Wno-reorder -g -gdwarf-2 -arch
x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -Wall -W
-DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB
So as you can see -Wall goes after my flags and discards them. What should I do to add those flags after ?
Don't use QMAKE_CXXFLAGS but rather override QMAKE_CXXFLAGS_WARN_ON with your own warnings:
QMAKE_CXXFLAGS_WARN_ON = -Wno-unused-variable -Wno-reorder