Qt requires C++11 support - qt

I used Qt 5.7 and gcc 4.9.2. Qt Core module throw Qt requires C++11 support error.
This page say that
gcc 4.9.2 fails to compile Qt.
So I installed gcc 4.8. I check using below command on terminal :
$ g++ --version
g++ (Ubuntu 4.8.4-1ubuntu15) 4.8.4
My kit uses cmake not qmake. I add TARGET_LINK_LIBRARIES ( xxxx yyyy /usr/bin/c++ -std=c++11 to CMakeLists-txt.
I restart my pc and run my application again. Same error is throwed.
/opt/Qt/5.7/gcc_64/include/QtCore/qbasicatomic.h:61: error: #error "Qt requires C++11 support"
# error "Qt requires C++11 support"
^
How can I solve it?

If using QtCreator, you can add this to your .pro file:
CONFIG += c++11
https://wiki.qt.io/How_to_use_C%2B%2B11_in_your_Qt_Projects

Its has been a while.
How I finally solve it is indicating in CMakeLists.txt the following line just after project(MyProject):
add_compile_options(-std=c++11)
That says to cmake, to create a Makefile that will use c++11 solving issues.

solution for me was (in your .pro file):
QMAKE_CXXFLAGS += -stdlib=libc++
QMAKE_CXXFLAGS += -std=c++11
QMAKE_CXXFLAGS += -mmacosx-version-min=10.7
QMAKE_LFLAGS += -mmacosx-version-min=10.7

Turn c++11 on explicitly:
set(CMAKE_CXX_FLAGS "-std=c++11" CACHE STRING "compile flags" FORCE) after project(...) declaration.
add_library(MyLib SHARED ${PROJECT_HEADERS} ${PROJECT_SOURCES})
...
set_property(TARGET MyLib PROPERTY CXX_STANDARD 11)
set_property(TARGET MyLib PROPERTY CXX_STANDARD_REQUIRED ON)

SOMETIMES, this will not be a configuration issue as mentioned in other answers. In my case, the problem was one file that happened to have been saved with a .CPP extension rather than .cpp. QMake (Qt5) was misidentifying the file and trying to compile it with the C compiler rather than the C++ compiler. The QMake from Qt4 was not exhibiting this issue. Renaming the file fixed the issue.
My comment at the time was "Could this really be that f&%%& simple!!"

Related

dlib with QT MinGW slow even in Release mode

I am compiling dlib 18.18 on Windows 10 for QT 5.6 with MinGW 4.9.2.
I have read all recommendations (http://dlib.net/faq.html#Whyisdlibslow): AVX or SSE4 instructions, Release mode.
The example "webcam_face_pose_ex.exe" works like a charm and really fast.
The problem:
But when I use the frontal_face_detector under QT the framerate is really low.
640x480 ~170 ms
1920x1080 ~1100 ms
System configuration: Windows 10 (x64), Intel Core i5-3550, QT 5.6, OpenCV 3.1.0, MinGW 4.9.2
Tried all variations of using AVX/SSE4 instructions in dlib and opencv.
But I think that it's something different - since the example is fast.
Code used in QT is copy-pasted from the example as well, even without the GUI.
In Debug mode 640x480 frames take 6-7 seconds to process.
.pro file dump:
DIR_DLIB = "$${LIBSDIR}dlib/dlib18.18/"
DEFINES += DLIB_ENABLE_ASSERTS
LIBS += -luser32 -lws2_32 -lgdi32 -lcomctl32 -limm32 -lwinmm
INCLUDEPATH += "$${DIR_DLIB}include"
DEPENDPATH += "$${DIR_DLIB}include"
LIBS += -L"$${DIR_DLIB}lib"
LIBS += -ldlib
Dlib face detector is header-based and does not depend on the compiler flags used to compile dlib.lib itself.
Dlib has a documented option "-DUSE_AVX_INSTRUCTIONS=ON", but it will not work if you are not building with CMAKE and including dlib/cmake file into your CMakeLists.txt
You should add AVX enabling flags for compiler into your project to make it work fast
The solution is to add an explicit parameter to your .pro file:
QMAKE_CXXFLAGS_RELEASE += -mavx
When you are compiling project in Qt Creator, you can see how does it call compiler and what flags are used. Ensure you have -mavx flag. -msse2 will work about 20% slower

Library function behaves differently if compiled in Qt Creator

I have a c program, that if I build it in the shell with this command:
gcc -o simpledemo -fpic -fsigned-char -DPLATFORM_LINUX -Iinclude/ simpledemo.c ../AcapelaLibs/libbabile.a -lstdc++
it compiles, runs, and produces the expected output.
gcc info:
/usr/bin/gcc
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4
However, if I compile it with Qt Creator (Qt 5.5.1), it compiles and runs but a NULL value is returned from a function of the library I use.
Qt uses the following g++:
/usr/bin/g++
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4
I think it might be something with the compiler flags. This is what I'm doing in Qt to match the shell command:
in .pro file:
QMAKE_CXXFLAGS += -DPLATFORM_LINUX
INCLUDEPATH += $$PWD/include/
I think the INCLUDEPATH is OK because I can access files there from within my code (they are recognizable).
And note that in the shell compilation I use a library ../AcapelaLibs/libbabile.a, so in Qt I click Add Library... and choose that same library, and Qt adds it to the .pro file. Also here I can access library functions from within my code.
The problem is that a function from libbabile.a returns NULL if I compile and run from Qt (or compile in Qt and run from shell).
What's the difference between the 2 methods that makes one of them succeed and the other fail?
Library function behaves differently if compiled in Qt Creator. I
think it might be something with the compiler flags.
What compiler flags are different from used in command line?
You can examine Qt Creator compiler output and get all the compiler options specified from there. Then you can compare those with expected set of options and deliberately add or remove those options in your project .pro file:
# C++ flags
QMAKE_CXXFLAGS += -opt1 -opt2 # add
QMAKE_CXXFLAGS -= -opt1 -opt2 # remove
QMAKE_CXXFLAGS_RELEASE += -opt1 -opt2 # add
QMAKE_CXXFLAGS_RELEASE -= -opt1 -opt2 # remove
QMAKE_CXXFLAGS_DEBUG += -opt1 -opt2 # add
QMAKE_CXXFLAGS_DEBUG -= -opt1 -opt2 # remove
# C flags, slightly different macro
QMAKE_CFLAGS += -opt1 -opt2 # mind add/remove/debug/release
For both debug and release modes and separately.

How to provide linker options when linking a static library with qmake?

I want to provide options to the linker when building a static library using qmake. Say I'd want to get verbose linker output when building with MSVC. The project file looks as follows:
# mylib.pro
TEMPLATE = lib
TARGET = mylib
CONFIG += staticlib
QT += core
win32-msvc*: QMAKE_LFLAGS += /VERBOSE
unix: QMAKE_LFLAGS += -v
That's the entire project file. It should result in an empty static library with no objects in it.
Setting neither QMAKE_LFLAGS nor QMAKE_LFLAGS_STATIC_LIB nor LIBS has any effect on the linker. Nothing set in those variables even makes it to the Makefile. If QMAKE_LFLAGS worked, I'd expect to see /VERBOSE or -v passed to the linker on the command line, as appropriate for given platform.
It doesn't matter what makefile generator is used, this behavior seems to be consistent. The two platforms of interest are.
qmake -spec win32-msvc2008
qmake -spec macx-llvm
Due to cross-platform nature of qmake, you can test it on any platform where you happen to have Qt installed. This reproduces on qmake from both Qt 4.8.4 and 5.1.1. The msvc version given in the mkspec doesn't matter.
In staticlib projects, the LFLAGS are not passed to the linker. In fact, there's no documented way to pass such flags.
The solution is generator-dependent.
For msvc_nmake, LIBFLAGS are passed to the linker instead. To get verbose output, you might add
QMAKE_LIBFLAGS += /VERBOSE
To verify that it works, on any system, you can invoke qmake -spec win32-msvc2008; the particular msvc version doesn't matter.
For unixmake, AR is used to invoke the linker, so you have to add the flags to QMAKE_AR. To get verbose output, you might add
QMAKE_AR += -v
To verify, invoke qmake -spec macx-llvm; any other unix spec should work as well.

Linking Matlab shared library into Qt (Windows)

I want to use Matlab's C API within QT (http://www.mathworks.com/help/techdoc/matlab_external/f39876.html#bsfvqhp-1) under Windows for opening a .mat file. In my .pro file I have included
INCLUDEPATH += "C:\Program Files\MATLAB\R2010b\extern\include"
which works fine (the code compiles). But when trying to link the libmat.lib file (I have read the .dll files cannot be linked directly) using
LIBS += -L"C:\Program Files\MATLAB\R2010b\extern\lib\win32\microsoft" -llibmat
the application crashes on execution. The error given says [file].exe exited with code -1073741515
I'm neither a QT nor a Windows expert but for this project I am forced to use both (I guess it would be easier to fix this in GNU/Linux) so any help would be appreciated. Using Windows XP, QT version 4.7.0 with Qt Creator 2.0.1, and Matlab R2010b.
The last output from the compiler just in case it is useful:
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows -o debug/MainUI.exe debug/main.o debug/maingui.o debug/matparser.o debug/matutils.o debug/moc_maingui.o -L'c:/Qt/2010.05/qt/lib' -lmingw32 -lqtmaind "-LC:\Program Files\MATLAB\R2010b\extern\lib\win32\microsoft" -llibmat -lQtGuid4 -lQtCored4
I just tested building a simple C program that uses the MAT-File Interface Library with no problems. The example file is located in: matlabroot/examples/eng_mat/matcreat.c. I am compiling using MinGW on a Windows XP 32-bit machine. Here is the Makefile I used:
# root directory of MATLAB installation
MATLABROOT="/c/Program Files/MATLAB/R2010b"
.PHONY : all clean run
all: matcreat
matcreat:
gcc ${MATLABROOT}/extern/examples/eng_mat/matcreat.c -o matcreat \
-I${MATLABROOT}/extern/include \
-L${MATLABROOT}/extern/lib/win32/microsoft -llibmat -llibmx
clean:
rm -rf matcreat *.exe *.mat
run:
# UNIX uses LD_LIBRARY_PATH to find libs at runtime, Windows/MinGW uses PATH
#PATH=${MATLABROOT}/bin/win32:"${PATH}" ./matcreat
I suspect that the Matlab library will have been compiled with MSVC and since you say you are compiling your own code with MingW I would imagine the two are incompatible.
Have a look at the MingW page on mixing compilers for more information.

Help getting started with Qt

I am a newby in QT and cannot even get a starting example to work. The problem is that even QtCore is not found. Think something wrong with my path or not the right version is used?
#include <QtCore>
#include <iostream>
#include <QtXml/QXmlSimpleReader>
int main(int argc, char *argv[])
{
QDir xmldir("/xxx/xxx");
QXmlSimpleReader xmlReader;
}
Example of error is:
/Users/frank/xxx-build-desktop/../xxx/main.cpp:1: error: QtCore: No such file or directory.
Checked the path and the path to qmake is /usr/bin/qmake
If I run qmake -v this is printed:
QMake version 2.01a
Using Qt version 4.7.0 in /Library/Frameworks
I am using Mac 10.6.6
Any reply will be appreciated.
Here is the project file:
QT += core gui
QT += xml
QT += webkit
QT += xmlpatterns
TARGET = xxx
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
Results from make:
iMac:~/xxx/ qmake -spec macx-g++
iMac:~/xxx/ ls -ltr
total 56
-rw-rw---- 1 frank staff 349 Feb 5 16:06 xxx.pro
-rw-rw---- 1 frank staff 3875 Feb 6 12:56 main.cpp
-rw-rw---- 1 frank staff 7985 Feb 6 12:56 xxx.pro.user
-rw-rw---- 1 frank staff 8974 Feb 6 12:56 Makefile
iMac:~/xxx/ make
g++ -c -pipe -g -gdwarf-2 -Wall -W -DQT_WEBKIT_LIB -DQT_XMLPATTERNS_LIB -DQT_XML_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/Qt4.7/mkspecs/macx-g++ -I. -I. -I/usr/include/QtCore -I. -I/usr/include/QtGui -I. -I/usr/include/QtXml -I. -I/usr/include/QtXmlPatterns -I. -I/usr/include/QtWebKit -I/usr/include -I. -F/Library/Frameworks -o main.o main.cpp
main.cpp:1:18: error: QtCore: No such file or directory
main.cpp: In function ‘int main(int, char**)’:
main.cpp:27: error: ‘QDir’ was not declared in this scope
main.cpp:27: error: expected `;' before ‘xmldir’
main.cpp:28: error: ‘xmldir’ was not declared in this scope
main.cpp:28: error: ‘QDir’ is not a class or namespace
main.cpp:28: error: ‘QDir’ is not a class or namespace
main.cpp:28: error: ‘QDir’ is not a class or namespace
main.cpp: At global scope:
main.cpp:25: warning: unused parameter ‘argc’
main.cpp:25: warning: unused parameter ‘argv’
main.cpp:63: warning: unused parameter ‘namespaceURI’
main.cpp:63: warning: unused parameter ‘localName’
main.cpp:63: warning: unused parameter ‘qName’
main.cpp:67: warning: unused parameter ‘namespaceURI’
main.cpp:67: warning: unused parameter ‘localName’
make: *** [main.o] Error 1
iMac:~/xxx/
For most C++ Qt applications you write, it's not immediately obvious how to compile the application by hand. This is complicated by a number of factors:
The header files may be placed in different directories depending on their version
moc, the meta-object compiler, needs to be run
The correct version of the DLLs need to be included
A number of preprocessor defines need to be in place
Given all of the above, it's easiest to use a build system like qmake, which is native to Qt, cmake, or some other build system that is Qt aware.
For people new to Qt, I recommend qmake.
Here's the basic command line usage. Qmake provides integration with both Visual Studio and XCode, but I won't address that here:
Create a directory for your project
Write whatever files you believe you need
Build a project file for qmake by running qmake on your project as follows: qmake -project "CONFIG+=xml". Since you're on mac, you might also want a -macx option, but I've never used it myself. At least in the past, qmake was not smart enough to pick up the XML dependency, so the "CONFIG+=xml" option adds it explicitly.
Create the makefiles from the project file by running qmake again: qmake
Now that you have makefiles, build the application using make, nmake, gmake, or the corresponding version of make for your system.
If everything has been picked up correctly by qmake, you should now be able to build your application by using make.
When you add new files to your project, they will need to be added to your project file (*.pro) and then qmake rerun without any options.
#includes are for header files. QtCore isn't a header, it's a directory that contains headers.
Since you're using QDir, change the first line to this:
#include <QtCore/QDir>
It is rather strange that your /usr/include/* directores don't exist. It seems that there is something wrong with the Qt installation. I don't understand Mac specifics, but from what I've been able to find out, it looks like the -F/Library/Frameworks parameter that make passes to G++ should allow you to use #include <QtCore/QtCore> instead of just #include <QtCore>. In a correct Qt installation on other platforms (Windows, Linux), both forms are equally acceptable though. But judging by the fact that G++ doesn't complain about the #include <QtXml/QXmlSimpleReader> line, the form #include <QtModule/ModuleHeader> works fine in your setup, so it could be used as a workaround, provided that there are no other problems.
Note that I wouldn't recommend to include QtCore in any form, though. It includes a lot of stuff most of which is probably useless to you and it will only increase compilation time and the probability of name conflicts. Include only what you intend to use instead, like #include <QDir> or #include <QtCore/QDir> in your case.
If you are new to Qt, and there's not special build requirement (PPC, 10.4, etc), I would strongly recommend that you download prepackaged Qt SDK of your platform. And use the included IDE, Qt Creator, to start. That will get your feet wet quickly and less painfully.
The variable settings you provided, which seems to be in your makefile, should actually be in a QMake file xxx.pro. You then run qmake, which generates the Makefile from xxx.pro, and then finally can run make. QMake uses variables such as QT and SOURCES, along with its knowledge of Qt and your installation, to produce a correct makefile.
If you do not find QtCore in /usr/include/, then this is an installation issue. In 4.7.0 I heard people having the same problem, so I guess there is a packaging problem for macs in 4.7.0.
Try to get 4.7.1 or build your own out of the sources.

Resources