Can not use C++11 in Qt - qt

Since Qt 5.7, C++11 support is mandatory. For some reason one large old project does not accept the C++11 arguments. It doesn't matter if I use "CONFIG += c++11" or any other; C++11 only features do not work and therefor I can not compile the project.
C:\QtE\5.7\android_armv7\include\QtCore\qbasicatomic.h:61: error:
error "Qt requires C++11 support"
^
The compiler arguments do contain "-std=c++11 -std=c++0x" if I use "CONFIG += c++11" in the .pro file. The target platform is Android ArmV7. SDK R24 and NDK R10E are used.
C:\SDKs\Android\android-ndk-r10e/toolchains/arm-linux-androideabi-4.9/prebuilt/windows/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 -fPIC -ansi -fpermissive -finline-functions -Wno-long-long -g -g -marm -O0 -fno-omit-frame-pointer -O2 -Wall -Wno-psabi -W -D_REENTRANT -fPIC -DPJ_IS_LITTLE_ENDIAN=1 -DPJ_IS_BIG_ENDIAN=0 -DQZXING_LIBRARY -DZXING_ICONV_CONST -DDISABLE_LIBRARY_FEATURES -DNOFMAXL -DBOTAN_DLL=Q_DECL_EXPORT -DBOTAN_TARGET_OS_HAS_GETTIMEOFDAY -DBOTAN_HAS_ALLOC_MMAP -DBOTAN_HAS_ENTROPY_SRC_DEV_RANDOM -DBOTAN_HAS_ENTROPY_SRC_EGD -DBOTAN_HAS_ENTROPY_SRC_FTW -DBOTAN_HAS_ENTROPY_SRC_UNIX -DBOTAN_HAS_MUTEX_PTHREAD -DBOTAN_HAS_PIPE_UNIXFD_IO -DBOTAN_TARGET_OS_IS_LINUX -DBOTAN_TARGET_OS_HAS_CLOCK_GETTIME -DBOTAN_TARGET_OS_HAS_DLOPEN -DBOTAN_TARGET_OS_HAS_GMTIME_R -DBOTAN_TARGET_OS_HAS_POSIX_MLOCK -DBOTAN_HAS_DYNAMICALLY_LOADED_ENGINE -DBOTAN_HAS_DYNAMIC_LOADER -DBOTAN_BUILD_COMPILER_IS_GCC -DQT_QML_DEBUG -DQT_QUICKWIDGETS_LIB -DQT_WIDGETS_LIB -DQT_QUICK_LIB -DQT_MULTIMEDIA_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_WEBSOCKETS_LIB -DQT_NETWORK_LIB -DQT_POSITIONING_LIB -DQT_ANDROIDEXTRAS_LIB -DQT_CORE_LIB
Any idea is welcome :)

According to this questions accepted answer, It says CONFIG += c++11 requires Qt 5. Are you sure you are using Qt 5?
Also, It also says if you use gcc/clang you should use QMAKE_CXXFLAGS += -std=c++11 (or QMAKE_CXXFLAGS += -std=c++0x).
since you are building an android project, it will use gcc. Change CONFIG += c++11 to QMAKE_CXXFLAGS += -std=c++11 and tell me what happened.

One of the .pri sub-projects used the argument "-ansi" which is equivalent to "-std=c++98". This prevents the compilation with C++11.
Thanks to Oktalist for the solution comment :)

Related

qt compile error including QtCore resulting in utf-8 error

Getting the following error in my library when trying to compile a library that is linking against Qt.
/opt/local/libexec/qt5/lib/QtCore.framework/QtCore:1:1: error: source file is not valid UTF-8
File.cxx is
#include <QtCore>
int main(){
return 0;
}
Using mac ports
Unsure if this is some weird mac problem or?
/opt/local/bin/clang++-mp-11 -DQT_CORE_LIB -D_LINUX_OS_ -D_XOPEN_SOURCE=600 -I/opt/local/libexec/qt5/include -isystem /opt/local/libexec/qt5/lib/QtCore.framework -isystem /opt/local/libexec/qt5/lib/QtCore.framework/Headers -isystem /opt/local/libexec/qt5/./mkspecs/macx-clang -g -std=c99 -pedantic -Wall -Wstrict-prototypes -Wwrite-strings -Wpointer-arith -Wcast-align -Werror -fPIC -std=gnu++11 -MD -MT fie.cxx.obj -MF file.cxx.obj.d -o file.cxx.obj -c file.cxx
Ive gotten similar results using xcode's compiler
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/CC

CMake fails to add fPIC compile option

I am having some issues trying to link an executable which transitively uses Qt5.
I have configured my project using CMake with the option CMAKE_POSITION_INDEPENDENT_CODE = ON but still, when compiling the executable, Qt5 complains that I should use fPIC.
[ 99%] Building CXX object tools/deSimRunner/CMakeFiles/deSimRunner.dir/main.cpp.o
cd /home/jjcasmar/projects/Hybrid/Desilico/prj/Release/tools/deSimRunner && /home/jjcasmar/usr/local/bin/c++ -DBOOST_ALL_DYN_LINK -DFMT_SHARED -DHAVE_CUDA -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DSPDLOG_COMPILED_LIB -DSPDLOG_FMT_EXTERNAL -D_GLIBCXX_USE_CXX11_ABI=1 -I/home/jjcasmar/projects/Hybrid/Desilico/src/deCore -I/home/jjcasmar/projects/Hybrid/Desilico/src/deCore/.. -I/home/jjcasmar/projects/Hybrid/Desilico/src/deGeom/.. -I/opt/cuda/include -I/home/jjcasmar/projects/Hybrid/Desilico/src/deSim/.. -isystem /home/jjcasmar/.conan/data/eigen/3.3.7/conan/stable/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include/eigen3 -isystem /home/jjcasmar/.conan/data/rapidjson/1.1.0/bincrafters/stable/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include -isystem /home/jjcasmar/.conan/data/boost/1.71.0/conan/stable/package/393cfc058d5be864014f06fc0bb0e29c6845d9e9/include -isystem /home/jjcasmar/.conan/data/zlib/1.2.11/conan/stable/package/1d877a3df840030e6a8abb74c5ffb9088d08b47a/include -isystem /home/jjcasmar/.conan/data/bzip2/1.0.8/conan/stable/package/a5875aed3fc7ae8dd0488f7e5e99acbc480d721d/include -isystem /usr/include/qt -isystem /usr/include/qt/QtCore -isystem /usr/lib/qt/mkspecs/linux-g++ -isystem /home/jjcasmar/.conan/data/spdlog/1.4.2/bincrafters/stable/package/f31849fd5982a882fc905666fe297f0c231b10af/include -isystem /home/jjcasmar/.conan/data/fmt/6.0.0/bincrafters/stable/package/ca4b767114c8e107f72671a3380d9917e2b9adff/include -isystem /home/jjcasmar/.conan/data/OpenMesh/7.1/desilico/stable/package/b80d46004713aa37d6a90b42e2a326a056a237b5/include -isystem /home/jjcasmar/.conan/data/nanoflann/1.3.0/desilico/stable/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include -isystem /home/jjcasmar/usr/local/include -isystem /home/jjcasmar/.conan/data/cereal/1.2.2/conan/stable/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include -isystem /home/jjcasmar/.conan/data/glm/0.9.9.5/g-truc/stable/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include -isystem /usr/include/qt/QtGui -isystem /home/jjcasmar/.conan/data/range-v3/0.9.1/ericniebler/stable/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9/include -fopenmp -O3 -DNDEBUG -fPIE -DNON_COMMERCIAL_LICENSE -std=gnu++14 -o CMakeFiles/deSimRunner.dir/main.cpp.o -c /home/jjcasmar/projects/Hybrid/Desilico/tools/deSimRunner/main.cpp
In file included from /usr/include/qt/QtCore/qjsonvalue.h:43,
from /usr/include/qt/QtCore/qjsondocument.h:43,
from /usr/include/qt/QtCore/QJsonDocument:1,
from /home/jjcasmar/projects/Hybrid/Desilico/tools/deSimRunner/main.cpp:23:
/usr/include/qt/QtCore/qglobal.h:1204:4: error: #error "You must build your code with position independent code if Qt was built with -reduce-relocations. " "Compile your code with -fPIC (-fPIE is not enough)."
# error "You must build your code with position independent code if Qt was built with -reduce-relocations. "\
I have these two options enabled in my main CMakeLists.txt file
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(BUILD_SHARED_LIBS ON)
How should I correctly add the fPIC option for my final executable?
Your error states:
"You must build your code with position independent code if Qt was built with -reduce-relocations. " "Compile your code with -fPIC (-fPIE is not enough)."
However, your compilation flags only contain fPIE, not fPIC:
/usr/local/bin/c++ -DBOOST_ALL_DYN_LINK ...
...
-fopenmp -O3 -DNDEBUG -fPIE -DNON_COMMERCIAL_LICENSE -std=gnu++14 -o CMakeFiles/deSimRunner.dir/main.cpp.o -c /home/jjcasmar/projects/Hybrid/Desilico/tools/deSimRunner/main.cpp
You were correct to try using CMAKE_POSITION_INDEPENDENT_CODE, but this variable may not set the flags you expect. The fairly undocumented behavior for this variable is this:
If the target is a library, the flag -fPIC is added by CMake to the compilation and linker steps.
If the target is an executable, the flag -fPIE is added by CMake to the compilation and linker steps.
Thus, you will have to add the -fPIC flag for your executable somewhat manually, with something like this:
add_executable(deSimRunner ... )
target_compile_options(deSimRunner PRIVATE -fPIC)

How to disable C++11 in qmake?

Project file:
CONFIG -= qt
QMAKE_CXXFLAGS += -std=c++98
QMAKE_CXXFLAGS -= -std=c++11
QMAKE_CXXFLAGS -= -std=gnu++11
CONFIG -= c++11
Result:
g++ -c -pipe -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-but-set-variable -Wno-reorder -Wno-missing-field-initializers -std=c++98 -DDEBUG -g -std=gnu++11 -Wall -W -fPIC -DQT_QML_DEBUG -I../../qqq -I. -I../src/jsoncpp -I../lib -I../../../../Qt/5.9.1/gcc_64/mkspecs/linux-g++ -o ../build/debug/obj/TaskManager.o ../src/TaskManager.cpp
g++ -c -pipe -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-but-set-variable -Wno-reorder -Wno-missing-field-initializers -std=c++98 -DDEBUG -g -std=gnu++11 -Wall -W -fPIC -DQT_QML_DEBUG -I../../qqq -I. -I../src/jsoncpp -I../lib -I../../../../Qt/5.9.1/gcc_64/mkspecs/linux-g++ -o ../build/debug/obj/Utils.o ../src/Utils.cpp
As you see option -std=gnu++11 still exist (no any reactoion to "QMAKE_CXXFLAGS -="/"CONFIG -=").
Qt:
Qt Creator 4.3.1
Based on Qt 5.9.1 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit)
Built on Jun 29 2017 04:10:39
Thank you a lot.
P.S. I have compatibility troubles with gnu++11 (c++ with gcc extenstions) on clean system
./qqq: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by ./qqq)
./qqq: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by ./qqq)
./qqq: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.21' not found
and want to use c++11 (c++ without gcc extenstions) instead gnu++11 without any extenstions but I cannot to disable it :-(

qmake adds /usr/lib absolute path when cross compiling

I need to cross compile a Qt app for arm. This is what I do:
export SDK_MXC_PATH="/path/to/sdk"
export QMAKESPEC="${SDK_MXC_PATH}/usr/mkspecs/devices/linux-imx6-g++/"
export SDK_MXC_BIN="${SDK_MXC_PATH}/toolchain/opt/freescale/usr/local/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/"
export QMAKE="${SDK_MXC_BIN}/qmake"
export PATH="${SDK_MXC_BIN}:${PATH}"
${QMAKE} -spec ${QMAKESPEC}
make
The mkspec file seems to be fine:
include(../common/linux_device_pre.conf)
EGLFS_PLATFORM_HOOKS_SOURCES = $$PWD/qeglfshooks_imx6.cpp
QMAKE_INCDIR += $(SDK_MXC_PATH)/usr/include
QMAKE_LIBDIR += $(SDK_MXC_PATH)/usr/lib
QMAKE_LIBS_EGL += -lEGL -L$(SDK_MXC_PATH)/usr/lib
QMAKE_LIBS_OPENGL_ES2 += -lGLESv2 -lEGL -lGAL -L$(SDK_MXC_PATH)/usr/lib
QMAKE_LIBS_OPENVG += -lOpenVG -lEGL -lGAL -L$(SDK_MXC_PATH)/usr/lib
QMAKE_LIBDIR_OPENGL_ES2 = $(SDK_MXC_PATH)/usr/lib
QMAKE_LFLAGS += -Wl,-rpath-link=$(SDK_MXC_PATH)/usr/lib
QMAKE_CFLAGS_RELEASE += -DLINUX=1 -DEGL_API_FB=1 -O3 -march=armv7-a -mcpu=cortex-a9 -mtune=cortex-a9 -mfloat-abi=softfp -mfpu=neon -ftree-vectorize -fomit-frame-pointer
QMAKE_CXXFLAGS_RELEASE += -DLINUX=1 -DEGL_API_FB=1 -O3 -march=armv7-a -mcpu=cortex-a9 -mtune=cortex-a9 -mfloat-abi=softfp -mfpu=neon -ftree-vectorize -fomit-frame-pointer
include(../common/linux_device_post.conf)
load(qt_config)
Linking fails, with this error:
arm-none-linux-gnueabi-g++ -c -pipe -O2 -DLINUX=1 -DEGL_API_FB=1 -O3 -march=armv7-a -mcpu=cortex-a9 -mtune=cortex-a9 -mfloat-abi=softfp -mfpu=neon -ftree-vectorize -fomit-frame-pointer -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../path/to/sdk/usr/mkspecs/devices/linux-imx6-g++ -I. -I../../path/to/sdk/usr/include -I../../path/to/sdk/usr/include/QtWidgets -I../../path/to/sdk/usr/include/QtGui -I../../path/to/sdk/usr/include/QtCore -I. -I/path/to/sdk/usr/include -o main.o main.cpp
arm-none-linux-gnueabi-g++ -Wl,-rpath-link=/path/to/sdk/usr/lib -Wl,-O1 -Wl,-rpath,/usr/lib -o qt-hello main.o -L/path/to/sdk/usr/lib -L/path/to/sdk/usr/lib -lQt5Widgets -L/usr/lib -lQt5Gui -lQt5Core -lGLESv2 -lEGL -lGAL -lpthread
/usr/lib/libc_nonshared.a: could not read symbols: File format not recognized
collect2: ld returned 1 exit status
Makefile:183: recipe for target 'qt-hello' failed
make: *** [qt-hello] Error 1
I guess because it is trying to search libraries in /usr/lib
If I manually call the linker, removing -L/usr/lib it works and the executable can run on my target. The command below works, notice that I've removed -L/usr/lib
arm-none-linux-gnueabi-g++ -Wl,-rpath-link=/path/to/sdk/usr/lib -Wl,-O1 -Wl,-rpath,/usr/lib -o qt-hello main.o -L/path/to/sdk/usr/lib -L/path/to/sdk/usr/lib -lQt5Widgets -lQt5Gui -lQt5Core -lGLESv2 -lEGL -lGAL -lpthread
What can I do make qmake working as expected? I don't want to link manually or edit the Makefile each time it is generated by qmake.
Just having the same problem w/ compiling Qt itself - seems to affect anything that calls mesa. But mesa's .pc files are okay.
OTOH, Qt creates broken .pc files. Looking at their monstrous makefile generator code, it seems they ever understood that pkg-config handles dependencies on it's own - IOW: missed the whole point. (well, they
still didn't understand make either).
Yet the old story - still not at all suited for embedded systems.

How do I let Callgrind access my Qt project's source code?

I built a Qt project in Debug mode with Qt Creator, ran Callgrind to generate profiling data and tried loading it into Cachegrind. I noticed that I only see profiling information for Qt classes, so I figured I must have forgotten to turn on a crucial flag.
Qt Creator's qmake command looks like this:
qmake /path/to/project/MyProject/MyProject.pro -r -spec linux-g++-64 CONFIG+=debug
Sample compile output for one file:
g++ -c -m64 -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I../Flowchart -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -I../Flowchart -I. -o main.o ../Flowchart/main.cpp
The -g flag is there, so debug information should be available. I verified this by running gdb on the executable and trying to look at some random source code with l.
The Callgrind command I used was:
valgrind --tool=callgrind ./MyProject
What am I missing here?
You should do:
valgrind --tool=callgrind --trace-children=yes --demangle=yes ./MyProject
Also depending on the distribution of Linux you may have to make sure that you have the Debug information in the QT libraries you are linking against.

Resources