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

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"

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();
}

compile with clang from the command line: compatibility issues mac os X

I am compiling a c++14 project on MacOsX10.10 using cmake, clang++, boost and openCV (static linkage - compilation flags: -Wall -std=c++14 -O3). How can I make sure the program runs out-of-the-box in older MacOsX versions? (and in other mac computers as well?) I've tested the binary on an older macbook running os X 10.7 and it failed. With xcode it's possible to build a program against some particular SDK, can I do something similar from the command line?
P.S. This is a more general question, but the source code for this particular project can be found here: https://github.com/MarinosK/oiko-nomic-threads
You need to make sure that all dependencies are linked statically into your executable. So this not only includes the .a (static libraries or object archives) but also the C++ (and possibly C) standard libraries.
For example:
clang --std=c++14 -stdlib=libstdc++ main.cpp -o main thirdparty.a -static -lstdc++

setting up c++11 in qt creator in snow leopard with gcc 4.9

Although I tried what was suggested in How to enable C++11 in Qt Creator?
this did not work with my Qt Creator using Qt 4.8 in mac os x snow leopard 10.6.8. When compiling with GCC 4.9, adding:
QMAKE_CXXFLAGS += -std=c++11
I get
cc1plus: error: unrecognized command line option "-std=c++11"
Is this meant to be compatible by Qt 4.8 like the link above said? or not?
You can try to force the compiler you want to use in the .pro file:
QMAKE_CC = full path of gcc (for ex. /opt/local/bin/gcc49)
QMAKE_CXX = full path of g++
QMAKE_LINK = full path of g++
QMAKE_LINK_SHLIB_CMD = full path of g++

Errors in QT headers using clang

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

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