Errors in QT headers using clang - c++

Questions:
My question is thus: How do I build my QT project without turning off warnings altogether (or having to sort through a million purposeless ones to find my own)? Can I suppress warnings for just the QT headers?
Details:
Issue
A number of months ago, I started a QT project in QT-Creator. At the time I was using gcc 4.6. After a bit other priorities asserted themselves and I found myself without time to work on the project until now. In the interim I switched to using clang. When I configured my QT project to use clang -- which project compiled without warnings in g++ -- it generated some 263 warnings all within the QT headers themselves. Mostly sign-conversion and unreachable-code.
Attempts
To try and get around this I added -isystem /path/to/QT/include/dir based on this entry in the Clang User Manual, but it did not seem to affect anything. Though I am not certain, I think it is because my code #include's the QT headers by name, not by directory. While the solution to that might be to manually list every single QT header used (have not tried), it would mean I would have to update it every time I upgraded QT or used a new header. Surely there is a better solution.
As requested here is the actual compile command being executed:
clang++ -c -pipe -Qunused-arguments -Weverything -cxx-isystem /path/to/qt/4.8.3/include/ -g -D_REENTRANT -DQT_NO_KEYWORDS -DQT_SHARED -I/path/to/qt/x86_64/4.8.3/mkspecs/unsupported/linux-clang -I. -I.moc -I.ui -I/path/to/qt/4.8.3/include/ -o .obj/main.o main.cpp
Specs
I am using:
Linux 3.2.0-40-generic #64-Ubuntu SMP x86_64 GNU/Linux
Though others on my team use Windows
QT creator 2.6.2
QT 4.8.3
clang version 3.2 (trunk 165250) (llvm/trunk 165249)
Target: x86_64-unknown-linux-gnu

I will answer my own question because, as it turns out, it is a specific environmental quirk in this case.
I have two copies of the QT libraries on my dev machine, one system-wide and one project specific (included in the VCS). The project libraries do not have qmake included, so I used my system qmake, which appended a different path than I was including in my -isystem specifications. To solve this, I added
QMAKE_INCDIR_QT =
to qmake.conf (in qt/mkspecs/unsupported/linux-clang/)
Since someone else on the project had fanangled qmake into using the project libraries everywhere else.

For those who stumble upon this question with a more general problem than the author.
Try inserting:
LIBS_USED_FOR_QT = QtCore QtSql QtMultimediaWidgets QtSensors QtSvg QtXml QtPrintSupport QtWidgets QtQuick QtQml QtPositioning QtGui QtWebKitWidgets
for(somelib, $$list($$LIBS_USED_FOR_QT)) {
QMAKE_CXXFLAGS += -isystem $$(QTDIR)/lib/$${somelib}.framework/Versions/5/Headers/
QMAKE_CXXFLAGS += -isystem $$(QTDIR)/lib/$${somelib}.framework/Headers/
}
in your .pro file.
Addtionally avoid includes like #include <QtCore/QtCore>
writing #include <QtCore> instead
This tamed qt quite efficiently for me.
see also this source

Related

QT5 and Antlr4.8 not Compiling with Cmake and QT Creator

As in the above im trying to compile a QT5 project inside of QT Creator with cmake and want to add the antlr package.
Infos: Qt 5.12.8, Antlr 4.8, Compilers tried: mingw gcc|g++ , msvc 2015,2017
System: Windows / Linux both do not work
For including Antlr 4.8 i use the provided CPP Package to build Antlr from Source
This is the project i am working on.
https://github.com/MrDiver/ArmSimulator/tree/ArmParser/src/asmeditor/AsmEditor
i include antlr4 with the following https://github.com/MrDiver/ArmSimulator/blob/ArmParser/src/asmeditor/AsmEditor/CMakeLists.txt#L14-L21
and normally it works perfectly fine when i compile something with it. But this time when i try to include the header files for antlr i get this error message
FAILED: CMakeFiles/AsmEditor.dir/managers/processormanager.cpp.obj
H:\Programme\QT\Tools\mingw730_64\bin\g++.exe -DANTLR4CPP_STATIC -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DQT_WIDGETS_LIB -I. -IH:/MyStuff/PrivateProjects/CPPProjects/ArmSimulator/src/asmeditor/AsmEditor -IAsmEditor_autogen/include -Iantlr4_runtime/src/antlr4_runtime/runtime/Cpp/runtime/src -IH:/MyStuff/PrivateProjects/CPPProjects/ArmSimulator/src/asmeditor/AsmEditor/armparser/assembler -IH:/MyStuff/PrivateProjects/CPPProjects/ArmSimulator/src/asmeditor/AsmEditor/armparser/walker -IH:/MyStuff/PrivateProjects/CPPProjects/ArmSimulator/src/asmeditor/AsmEditor/codearea -IH:/MyStuff/PrivateProjects/CPPProjects/ArmSimulator/src/asmeditor/AsmEditor/managers -IH:/MyStuff/PrivateProjects/CPPProjects/ArmSimulator/src/asmeditor/AsmEditor/armparser -isystem H:/Programme/QT/5.12.8/mingw73_64/include -isystem H:/Programme/QT/5.12.8/mingw73_64/include/QtWidgets -isystem H:/Programme/QT/5.12.8/mingw73_64/include/QtGui -isystem H:/Programme/QT/5.12.8/mingw73_64/include/QtANGLE -isystem H:/Programme/QT/5.12.8/mingw73_64/include/QtCore -isystem H:/Programme/QT/5.12.8/mingw73_64/./mkspecs/win32-g++ -O3 -DNDEBUG -std=gnu++11 -MD -MT CMakeFiles/AsmEditor.dir/managers/processormanager.cpp.obj -MF CMakeFiles\AsmEditor.dir\managers\processormanager.cpp.obj.d -o CMakeFiles/AsmEditor.dir/managers/processormanager.cpp.obj -c H:/MyStuff/PrivateProjects/CPPProjects/ArmSimulator/src/asmeditor/AsmEditor/managers/processormanager.cpp
In file included from antlr4_runtime/src/antlr4_runtime/runtime/Cpp/runtime/src/antlr4-runtime.h:31:0,
from H:/MyStuff/PrivateProjects/CPPProjects/ArmSimulator/src/asmeditor/AsmEditor/managers/processormanager.cpp:2:
antlr4_runtime/src/antlr4_runtime/runtime/Cpp/runtime/src/Lexer.h:116:46: error: invalid declarator before 'newToken'
virtual void emit(std::unique_ptr<Token> newToken);
^~~~~~~~
antlr4_runtime/src/antlr4_runtime/runtime/Cpp/runtime/src/Lexer.h:116:46: error: expected ')' before 'newToken'
antlr4_runtime/src/antlr4_runtime/runtime/Cpp/runtime/src/Lexer.h:123:25: error: expected unqualified-id before ')' token
virtual Token* emit();
^
[30/31 0.6/sec] Building CXX object CMakeFiles/AsmEditor.dir/armparser/assembler/ARMParser.cpp.obj
ninja: build stopped: subcommand failed.
18:59:55: The process "C:\msys64\mingw64\bin\cmake.exe" exited with code 1.
Error while building/deploying project AsmEditor (kit: Desktop Qt 5.12.8 MinGW 64-bit)
When executing step "CMake Build"
File of Interest: https://github.com/MrDiver/ArmSimulator/blob/ArmParser/src/asmeditor/AsmEditor/managers/processormanager.cpp
Seems pretty much like it can't find std::unique_ptr<Token> but i can't change a lot about this, because these are the antlr sources and they actually work.
So i don't really know anymore where to search for the problem. The library itself compiles fine if i choose it as target in the settings.
What i tried:
Compiling the antlr library outside and just linking it in the cmake file.
Changing the Lexer.h file with #include <memory>.
Compiling the QT project without QT Creator (Resulting in a ton more errors because i cant get the linking for QT to work)
Compiling every Antlr related file outside of the project (Works perfectly fine without QT but not really what i need)
Using different compilers and different Systems.
Searching for a problem with moc but it doesn't seem to influence it. But i doubt i looked hard enough on moc for this.
And im quite new to QT so i don't know everything what the Qt Creator does behind the scenes. It seems like too much if i can't compile it with plain cmake out of the box.
Thanks for the help in advance.
The problem is that Antlr has methods like "emit" that conflict with the Qt reserved word "emit". The solution is to disable the flag "no_keywords" (in qmake add CONFIG += no_keywords, and in CMake add add_definitions(-DQT_NO_KEYWORDS)) as indicated by the docs. Then you must make the following conversions:
emit to Q_EMIT
slot to Q_SLOT
slots to Q_SLOTS
signal to Q_SIGNAL
signals to Q_SIGNALS

Qt creator not seeing Mac Frameworks

I am using Qt 5.6 with Qt Creator 4.3 on macOS Sierra 10.12.
I am building a Qt based application that uses OpenGL and OpenCL.
Software or system updates somewhere appear to have broken Qt Creators' ability to find the headers for OpenGL and OpenCL headers.
I have tried reinstalling Qt, Reinstalling Xcode and the Mac SDK and none of these options make it work.
The weird thing is my app compiles. QtCreator just cannot find the headers itself - this makes code navigation somewhat more complex.
So lets take OpenCL. I include it like this:
#ifdef __APPLE__
#include <OpenCL/cl.h>
#include <OpenCL/opencl.h>
#endif
My app.pro file for qmake has the following inside it:
macx:QMAKE_LFLAGS += -framework OpenCL
And with this it seems to compile. However the two OpenCL #include above in QtCreator are yellow underlined with a tooltip saying "OpenCL/cl.h: No such file or directory".
Now I can see that it compiles successfully as qmake is working out where the frameworks are and appending the paths successfully:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -c -pipe -stdlib=libc++ -g -std=gnu++11 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -mmacosx-version-min=10.7 -Wall -W -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/Users/me/Projects/app -I. -I/Users/me/Qt5.6.2/5.6/clang_64/lib/QtWidgets.framework/Headers -I/Users/me/Qt5.6.2/5.6/clang_64/lib/QtGui.framework/Headers -I/Users/me/Qt5.6.2/5.6/clang_64/lib/QtCore.framework/Headers -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/AGL.framework/Headers -I. -I/Users/me/Qt5.6.2/5.6/clang_64/mkspecs/macx-clang -F/Users/me/Qt5.6.2/5.6/clang_64/lib -o main.o /Users/me/Projects/app/main.cpp
Now if I navigate to the following directory the headers are there and present:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenCL.framework/Versions/A
So I cannot see what is missing.
I have tried to "teach" Qt Creator about this framework directory with combinations of the following, but I havent found something that will work:
macx:QMAKE_LFLAGS += -framework OpenCL
macx:INCLUDEPATH += /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenCL.framework/Versions/A/Headers
macx:QMAKE_LFLAGS += -F/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks
macx:LIBS += -framework OpenCL
I'm struggling to find information about how QtCreator find the system frameworks on a mac and how i get it to start finding them again. Any help would be massively appreciated.
For anybody that has the same problem the answer was actually rather simple in the end.
When you setup the kit in Qt creator (which tells it what compiler and debugger to use amongst other things) you need to be careful with the compiler used. I had two copies of clang on my machine, one was installed in /usr/bin (the system one) and the other was installed inside the XCode app bundle. My kit was using the Xcode installed one as thats where the libraries live. While this compiled just fine, Qt creator for some reason does not hook up the paths to libraries properly. Simply switching this over to using the one in /usr/bin solved it.
Note that i did not explicitly ask it to use the one in the Xcode app bundle, thats just the one that was automatically selected when the project was first opened in Qt creator. So you might have to adjust its default behaviour in order to fix things.

Compiling standalone Qt application using MinGW

I am trying to build a standalone Qt app without any DLLs needed. I recompiled Qt 5.4.1 statically. When I compile and run an application, it doesn't require any Qt DLLs, but it requires libgcc_s_dw2-1.dll instead. I have also edited my mkspecs before configuring and building Qt, I edited these values:
QMAKE_CFLAGS = -pipe -fno-keep-inline-dllexport -static -static-libgcc
QMAKE_CXXFLAGS = -pipe -fno-keep-inline-dllexport -static -static-libgcc -static-libstdc++
(added -static -static-libstdc++ and -static-libgcc)
I also added a QMAKESPECS environment variable.
When I build something using Qt, I can always see this options in the output, so I am sure that the mkspecs are applying.
When I build a non-Qt program with these options (-static -static-libgcc -static-libstdc++), it doesn't need any DLLs when I run it.
Can somebody help me?
I use Qt 5.4.1 and MinGW-w64 4.9.2
I solved my problem now. The problem was that although I edited the variable QMAKE_CXXFLAGS, it was still linking the standard libraries dynamically when linking the application itself, because it doesn't use this variable in the final step of the compilation. I only edited the mkspecs again and added the -static option to the variable QMAKE_LIBS and it works now, I have a standalone Qt application.

C++ PackageKit newb linking issue

I'm trying to import PackageKit into a C++ project I'm working with (as a C++ newbie coming from a mostly Java background). My goal is do some things with the packages I have installed on my system.
I've installed libpackagekit-glib2-16, libpackagekit-glib2-dev, libpackagekit-qt2-6, libpackagekit-qt2-dev, and packagekit (I know I won't need all of these down the line, but I'm just covering my bases for now). I can see that they've been installed here: /usr/include/PackageKit which has the subfolders packagekit-glib2, packagekit-qt2, plugin.
To help me along I'm using qt 5.2.1 to act as a crutch for my Makefiles while I'm still learning, but I'm not actually using any qt resources for now. I've been able to import apt's and dpkg's libraries previously via -lapt-lib and -ldpkg under qt's LIBS+= but I can't figure out how to import the packagekit's library (I've tried multiple variations, but I can't figure out how to properly import this library).
This:
#define I_KNOW_THE_PACKAGEKIT_GLIB2_API_IS_SUBJECT_TO_CHANGE
#include <PackageKit/packagekit-glib2/packagekit.h>
int main(int argc, char *argv[])
{
return 0;
}
Results with this:
g++ -c -std=c++11 -g -Wall -W -D_REENTRANT -fPIE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I../console-example -I/usr/include/qt5 -I/usr/include/qt5/QtCore -I. -I. -o main.o ../console-example/main.cpp
In file included from ../console-example/main.cpp:3:0:
/usr/include/PackageKit/packagekit-glib2/packagekit.h:31:41: fatal error: packagekit-glib2/pk-catalog.h: No such file or directory
#include <packagekit-glib2/pk-catalog.h>
If this is necessary, my system is Ubuntu 14.04 64bit and as I've mentioned I'm using qt 5.2.1 to help with the makefile. Thanks to all in advance!
This seems to be a compilation problem. The compiler cannot find packagekit's headers. I suppose that into g++ line you need to add -I/usr/include/PackageKit or something similar.
From within my qt project's .pro file I had to make the following additions to get it to compile and link.
CONFIG += link_pkgconfig # This enables the next line
PKGCONFIG = gtk+-2.0 # This will link against gtk+-2.0
INCLUDEPATH += /usr/include/PackageKit/ \ # This is the include for packagekit
+= /usr/lib/glib-2.0/include/ # This will include glib, which packagekit is dependent on

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"