OpenGL + Qt using CMake - c++

I have a Qt project created with a *.pro file that I need to migrate it to a CMakeLists. This project uses a simple OpenGL animation to show a 3D model of a hand. I already change it to use CMake, but I encounter 2 problems. (The program compiles but it doesn't run properly)
The memory consumption of the program passes from being 20-50MB using the *.pro file, to 1.3GB using CMake (Maybe some library being loaded completely or something??)
The program runs incredibly slow (like 1 frame every 5-10 seconds) in contrast with the speed from using the *.pro file (approx. 3 frames per second)
The question is, what I am doing wrong and how can I fix it?
Here is the *.pro file:
QT += core gui opengl
TARGET = RGBD_3D_Viewer
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
glwidget.cpp \
glwidget_Camera.cpp \
glwidget_Comm.cpp \
glwidget_Extractors.cpp \
glwidget_Rendering.cpp \
glwidget_Video.cpp \
glwidget_UI_Mouse.cpp \
glwidget_OpenGL.cpp \
mainwindow_Comm.cpp \
mainwindow_GUI.cpp \
model.cpp \
cameraSet.cpp \
model_Mesh.cpp \
model_Skeleton.cpp \
model_Skin.cpp \
model_Extra_SkinningStuff.cpp \
animation.cpp \
animation_Transform.cpp \
videoSequence.cpp \
sequence.cpp \
mainwindow_UI_Keyboard_Mouse.cpp \
tracker.cpp \
mainwindow_FrameNumber.cpp \
model_Limits.cpp \
animation_Files_CompleteSequence.cpp \
mainwindow_MODELS_INFO.cpp \
modelSET.cpp \
animation_0_RotAxes_Limits.cpp \
myMATH.cpp \
types_Background.cpp \
model_Extra_VOI.cpp \
fingertipSet.cpp \
tracker_OnIndexChange.cpp \
tracker_wFeatureSet.cpp
HEADERS += mainwindow.h \
glwidget.h \
model.h \
cameraSet.h \
animation.h \
videoSequence.h \
sequence.h \
tracker.h \
mymath.h \
modelSET.h \
ui_mainwindow.h \
featureSet.h \
typesBackground.h \
fingertipSet.h
FORMS += mainwindow.ui
INCLUDEPATH += /usr/include/eigen3/
INCLUDEPATH += /home/cvg11/projects/development/RGBD_3D_Viewer/glm
LIBS += -L/usr/local/lib/
LIBS += -lopencv_core
LIBS += -lopencv_highgui
QMAKE_CXXFLAGS += -O3
QMAKE_CXXFLAGS += -frounding-math
#QMAKE_CXXFLAGS += -std=c++0x
Here is the CMakeLists.txt file:
project(3d_viewer)
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories( ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/glm)
find_package( PkgConfig )
pkg_check_modules( EIGEN3 REQUIRED eigen3 )
include_directories( ${EIGEN3_INCLUDE_DIRS} )
# Opencv required
find_package(OpenCV COMPONENTS core highgui REQUIRED)
include_directories(${OPENCV_INCLUDE_DIRS})
link_directories(${OPENCV_LIBRARY_DIRS})
add_definitions(${OPENCV_DEFINITIONS})
message("\n\nFound OpenCV\n\n")
# QT4 required
find_package(Qt4 COMPONENTS QtCore QtGui QtOpenGL REQUIRED)
set(QT_USE_QTOPENGL TRUE)
include(${QT_USE_FILE})
add_definitions(${QT_DEFINITIONS})
message("\n\nFound QT4\n\n")
INCLUDE_DIRECTORIES(${QT_QTOPENGL_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR} )
#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
file(GLOB VIEWER_SOURCES src/*.cpp)
file(GLOB VIEWER_INCLUDES include/*.h)
# set QT headers
SET(QT_HEADERS
include/mainwindow.h
include/glwidget.h
)
#set QT forms
SET(QT_FORMS
ui/mainwindow.ui
)
# create moc for QT
QT4_WRAP_CPP(QT_MOC ${QT_HEADERS})
# process ui
QT4_WRAP_UI(QT_FORMS_HEADERS ${QT_FORMS})
ADD_EXECUTABLE(3d_viewer ${VIEWER_SOURCES} ${VIEWER_INCLUDES}
${QT_HEADERS}
${QT_MOC}
${QT_FORMS})
TARGET_LINK_LIBRARIES(3d_viewer ${QT_LIBRARIES} ${OpenCV_LIBS} )
set_property(TARGET 3d_viewer PROPERTY COMPILE_DEFINITIONS QT_SHARED)
EDIT:
Here are the two outputs of make VERBOSE=1 (I just show the last link and one of the files since the rest of the files are the same)
CMake:
[100%] Building CXX object CMakeFiles/3d_viewer.dir/include/moc_glwidget.cxx.o
/usr/bin/c++ -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DQT_OPENGL_LIB -DQT_SHARED -O3 -DNDEBUG -I/home/cvg11/projects/development/RGBD_3D_Viewer/build -I/home/cvg11/projects/development/RGBD_3D_Viewer/include -I/home/cvg11/projects/development/RGBD_3D_Viewer/glm -I/usr/include/eigen3 -I/usr/local/include/opencv -I/usr/local/include -isystem /usr/include/qt4 -isystem /usr/include/qt4/QtOpenGL -isystem /usr/include/qt4/QtGui -isystem /usr/include/qt4/QtCore -o CMakeFiles/3d_viewer.dir/include/moc_glwidget.cxx.o -c /home/cvg11/projects/development/RGBD_3D_Viewer/build/include/moc_glwidget.cxx
Linking CXX executable ../bin/3d_viewer
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/3d_viewer.dir/link.txt --verbose=1
/usr/bin/c++ -O3 -DNDEBUG CMakeFiles/3d_viewer.dir/src/mainwindow_FrameNumber.cpp.o CMakeFiles/3d_viewer.dir/src/animation.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget_OpenGL.cpp.o CMakeFiles/3d_viewer.dir/src/main.cpp.o CMakeFiles/3d_viewer.dir/src/mainwindow_Comm.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget_Comm.cpp.o CMakeFiles/3d_viewer.dir/src/myMATH.cpp.o CMakeFiles/3d_viewer.dir/src/model.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget_Rendering.cpp.o CMakeFiles/3d_viewer.dir/src/model_Extra_VOI.cpp.o CMakeFiles/3d_viewer.dir/src/videoSequence.cpp.o CMakeFiles/3d_viewer.dir/src/cameraSet.cpp.o CMakeFiles/3d_viewer.dir/src/model_Extra_SkinningStuff.cpp.o CMakeFiles/3d_viewer.dir/src/mainwindow_MODELS_INFO.cpp.o CMakeFiles/3d_viewer.dir/src/animation_0_RotAxes_Limits.cpp.o CMakeFiles/3d_viewer.dir/src/modelSET.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget_Video.cpp.o CMakeFiles/3d_viewer.dir/src/animation_Transform.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget_Camera.cpp.o CMakeFiles/3d_viewer.dir/src/sequence.cpp.o CMakeFiles/3d_viewer.dir/src/animation_Files_CompleteSequence.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget_UI_Mouse.cpp.o CMakeFiles/3d_viewer.dir/src/model_Skin.cpp.o CMakeFiles/3d_viewer.dir/src/tracker_wFeatureSet.cpp.o CMakeFiles/3d_viewer.dir/src/tracker_OnIndexChange.cpp.o CMakeFiles/3d_viewer.dir/src/mainwindow.cpp.o CMakeFiles/3d_viewer.dir/src/types_Background.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget_Extractors.cpp.o CMakeFiles/3d_viewer.dir/src/model_Limits.cpp.o CMakeFiles/3d_viewer.dir/src/model_Skeleton.cpp.o CMakeFiles/3d_viewer.dir/src/tracker.cpp.o CMakeFiles/3d_viewer.dir/src/model_Mesh.cpp.o CMakeFiles/3d_viewer.dir/src/mainwindow_UI_Keyboard_Mouse.cpp.o CMakeFiles/3d_viewer.dir/src/fingertipSet.cpp.o CMakeFiles/3d_viewer.dir/src/mainwindow_GUI.cpp.o CMakeFiles/3d_viewer.dir/src/glwidget.cpp.o CMakeFiles/3d_viewer.dir/include/moc_mainwindow.cxx.o CMakeFiles/3d_viewer.dir/include/moc_glwidget.cxx.o -o ../bin/3d_viewer -L/usr/local/cuda/lib64 -rdynamic -lglut -lXmu -lXi -lQtOpenGL -lQtGui -lQtCore /usr/local/lib/libopencv_core.so.2.4.9 /usr/local/lib/libopencv_highgui.so.2.4.9 /usr/local/lib/libopencv_core.so.2.4.9 -Wl,-rpath,/usr/local/cuda/lib64:/usr/local/lib -Wl,-rpath-link,/usr/local/lib
make[2]: Leaving directory `/home/cvg11/projects/development/RGBD_3D_Viewer/build'
/usr/local/bin/cmake -E cmake_progress_report /home/cvg11/projects/development/RGBD_3D_Viewer/build/CMakeFiles 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
[100%] Built target 3d_viewer
*.pro project:
g++ -c -pipe -frounding-math -O3 -O2 -w -D_REENTRANT -DQT_WEBKIT -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtOpenGL -I/usr/include/qt4 -I/usr/include/eigen3 -I../../projects/development/RGBD_3D_Viewer/glm -I/usr/X11R6/include -I. -I. -o moc_mainwindow.o moc_mainwindow.cpp
/usr/bin/moc-qt4 -DQT_WEBKIT -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtOpenGL -I/usr/include/qt4 -I/usr/include/eigen3 -I../../projects/development/RGBD_3D_Viewer/glm -I/usr/X11R6/include -I. -I. glwidget.h -o moc_glwidget.cpp
g++ -c -pipe -frounding-math -O3 -O2 -w -D_REENTRANT -DQT_WEBKIT -DQT_OPENGL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtOpenGL -I/usr/include/qt4 -I/usr/include/eigen3 -I../../projects/development/RGBD_3D_Viewer/glm -I/usr/X11R6/include -I. -I. -o moc_glwidget.o moc_glwidget.cpp
g++ -Wl,-O1 -o RGBD_3D_Viewer main.o mainwindow.o glwidget.o glwidget_Camera.o glwidget_Comm.o glwidget_Extractors.o glwidget_Rendering.o glwidget_Video.o glwidget_UI_Mouse.o glwidget_OpenGL.o mainwindow_Comm.o mainwindow_GUI.o model.o cameraSet.o model_Mesh.o model_Skeleton.o model_Skin.o model_Extra_SkinningStuff.o animation.o animation_Transform.o videoSequence.o sequence.o mainwindow_UI_Keyboard_Mouse.o tracker.o mainwindow_FrameNumber.o model_Limits.o animation_Files_CompleteSequence.o mainwindow_MODELS_INFO.o modelSET.o animation_0_RotAxes_Limits.o myMATH.o types_Background.o model_Extra_VOI.o fingertipSet.o tracker_OnIndexChange.o tracker_wFeatureSet.o moc_mainwindow.o moc_glwidget.o -L/usr/lib/x86_64-linux-gnu -L/usr/X11R6/lib -L/usr/local/lib/ -lopencv_core -lopencv_highgui -lQtOpenGL -lQtGui -lQtCore -lGL -lpthread
I tried adding/removing the -frounding-math flag without having any appreciable difference...

You seem to be trying to change the optimization level value with qmake in the wrong way in here:
QMAKE_CXXFLAGS += -O3
The problem with this line is that g++ will use -O2 for the compiling phase and -O1 for the linking phase by default. You seem to want to change only the compiler phase as you do not specify the linker flags. However, += means addition with qmake, not override. The proper way to achieve your original goal would be this:
QMAKE_CXXFLAGS_RELEASE -= -O2
QMAKE_CXXFLAGS_RELEASE += -O3
and the following line to override the linker stage, too:
QMAKE_LFLAGS_RELEASE -= -O1
You will naturally need to rerun qmake after this change. Now, -O3 means that it will optimize for performance and not space. Therefore, your former concern about space may be due to this. The second part is still questionable, however, without concrete details.
The default optimization level for cmake is different to qmake; it is -O3. You can easily check that by running the following short cmake snippet:
message("CMAKE_CXX_FLAGS_RELEASE: ${CMAKE_CXX_FLAGS_RELEASE}")
You need to sync these up to bring them inline. For instance, if you want to use -O2 everywhere, overriding the cmake, too, you will need to apply something this:
set(CMAKE_CXX_FLAGS_RELEASE "-O2")
If you would like to use -O3, see the aforementioned logic for changing that in the qmake project file. If you would like to use something else like -Os, you will need to apply both types of changes. I think, that is pretty much about it.
As for debugging, you may want to completely turn out optimization in both cases to have a nicer debugging experience, however!
In general, you need to decide about the performance versus space trait. You seem to be complaining about both, but you inherently will not get perfectionism at both. If you want to fully optimize for space, use -Os, if for performance, use -O3, if you want a compromised solution, use something in-between, etc.

After fighting for days with this problem, I found out that it was the -O3 flag. Apparently, Qt is using -O3 -O2 and it is taking the last one, and for the final linking it is using -O1. I changed the flags to use -O2 and everything started to work as fast as it should be and using a normal amount of RAM.

Related

QT application will not compile when OpenCV 4.2 is used

I'm trying to build an application using OpenCV and QT, working out of QT creator. I've used this combination in the past from Visual Studio without any problems, but I have started using a Linux workstation as my main dev box, so have switched to QTCreator as an IDE.
The application receives data over the network and displays some of it as an image; all the other stuff works ok and has been tested; it's just showing the image that causes the problem.
The build error occurs when I call imshow:
Mat image = Mat(1944, 2592, CV_16U, (void*)images[imgCount].data());
imshow("disp", image);
And the build output looks like this (I have shortened some of the file paths):
g++ -c -pipe -g -std=gnu++11 -Wall -Wextra -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_NETWORK_LIB -DQT_CORE_LIB -I../RasPiRawImageReceiverTest -I. -isystem /usr/local/include/opencv4 -isystem /usr/include/qt -isystem /usr/include/qt/QtNetwork -isystem /usr/include/qt/QtCore -I. -I/usr/lib/qt/mkspecs/linux-g++ -o main.o ../RasPiRawImageReceiverTest/main.cpp
g++ -c -pipe -g -std=gnu++11 -Wall -Wextra -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_NETWORK_LIB -DQT_CORE_LIB -I../RasPiRawImageReceiverTest -I. -isystem /usr/local/include/opencv4 -isystem /usr/include/qt -isystem /usr/include/qt/QtNetwork -isystem /usr/include/qt/QtCore -I. -I/usr/lib/qt/mkspecs/linux-g++ -o receiver.o ../RasPiRawImageReceiverTest/receiver.cpp
g++ -pipe -g -std=gnu++11 -Wall -Wextra -dM -E -o moc_predefs.h /usr/lib/qt/mkspecs/features/data/dummy.cpp
/usr/bin/moc -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_NETWORK_LIB -DQT_CORE_LIB --include moc_predefs.h -I/usr/lib/qt/mkspecs/linux-g++ -I/QtCreatorProjects/RasPiRawImageReceiverTest -I/usr/local/include/opencv4 -I/usr/include/qt -I/usr/include/qt/QtNetwork -I/usr/include/qt/QtCore -I. -I/usr/include/c++/9.2.1 -I/usr/include/c++/9.2.1/x86_64-pc-linux-gnu -I/usr/include/c++/9.2.1/backward -I/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.1/include -I/usr/local/include -I/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.1/include-fixed -I/usr/include ../receiver.h -o moc_receiver.cpp
g++ -c -pipe -g -std=gnu++11 -Wall -Wextra -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_NETWORK_LIB -DQT_CORE_LIB -I../RasPiRawImageReceiverTest -I. -isystem /usr/local/include/opencv4 -isystem /usr/include/qt -isystem /usr/include/qt/QtNetwork -isystem /usr/include/qt/QtCore -I. -I/usr/lib/qt/mkspecs/linux-g++ -o moc_receiver.o moc_receiver.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:9:15: warning: unused variable ‘r’ [-Wunused-variable]
9 | Receiver* r = new Receiver();
| ^
g++ -o RasPiRawImageReceiverTest main.o receiver.o moc_receiver.o -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_videoio -lopencv_imgcodecs -lopencv_flann /usr/lib/libQt5Network.so /usr/lib/libQt5Core.so -lpthread
/usr/bin/ld: receiver.o: in function `Receiver::dataReady()':
receiver.cpp:52: undefined reference to `imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
collect2: error: ld returned 1 exit status
make: *** [Makefile:228: RasPiRawImageReceiverTest] Error 1
12:03:02: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project RasPiRawImageReceiverTest (kit: Desktop)
When executing step "Make"
OpenCV has been compiled from the 4.2.0 stable source files downloadable from the OpenCV website and using the default CMake config. QT 5.14.1 is installed from the Manjaro repos.
From what I can tell this a C++ standard mismatch, but as far as I can tell both OpenCV and the application are being compiled with C++11. The QT .pro file has the CONFIG += c++11 line in. Other OpenCV functions work; it just seems to be this from what I have tested. I know there are other warnings in the build, but these are not a problem at the moment.
Edit: .pro looks like this:
QT -= gui
QT += network
CONFIG += c++11 console
CONFIG -= app_bundle
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
receiver.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
receiver.h
INCLUDEPATH += \
/usr/local/include/opencv4
/usr/local/include
LIBS += \
-L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_videoio -lopencv_imgcodecs -lopencv_flann

How to include tensorflow c++ ".so" file in makefile?

I have created a .so file using the bazel command:
bazel build -c opt --copt="-fPIC" :tensorFlow.so
Following is my makefile:
CFLAGS = -c -g -W -O3 -Wall -Wshadow \
-Wno-long-long -Wpointer-arith -D_REENTRANT \
-D_POSIX_PTHREAD_SEMANTICS -DLINUX2 \
-I ./acl/lib_acl_cpp/include
BASE_PATH=./acl
LDFLAGS = -L$(BASE_PATH)/lib_acl_cpp/lib -l_acl_cpp \
-L$(BASE_PATH)/lib_protocol/lib -l_protocol \
-L$(BASE_PATH)/lib_acl/lib -l_acl \
-lpthread -lprotobuf -ljsoncpp
redisConnection: redisConnection.o
g++ ./redisConnection.o ./UnifiedMetric.pb.o ./getMapFromFeatureDistribution.o ./featureStats.o $(LDFLAGS) -o redisConnection
redisConnection.o: redisConnection.cpp
g++ $(CFLAGS) redisConnection.cpp -o redisConnection.o
I went through various links but couldn't find any way. How do I include the .so file file created in the makefile and what other things have to be added?
Add the paths to header files in INCLUDEPATH.
This might be the exact replica for your problem.

PRE_TARGETDEPS fails when using parallel builds

I'm using a custom ressource management (in replacement to qrc) and I'm trying to integrate it to QtCreaor.
I have a Python script that generates a source file to be compiled. I use QMAKE_EXTRA_TARGETS/PRE_TARGETDEPS to tell QMake that this script must be executed before files are compiled. So I do, in my pro file:
CONFIG += ordered
generated_file.target = my_custom_target
generated_file.commands = echo "Generating..." && d:/dev/vobs_ext_2015/tools_ext/python/Python34_light/python.exe $$PWD/pyc_res_generator.py -o $$PWD/generated/generated.cpp && echo "Generated!"
generated_file.depends = FORCE
QMAKE_EXTRA_TARGETS += generated_file
PRE_TARGETDEPS += my_custom_target
SOURCES += generated/generated.cpp
#DEPENDPATH = ./generated
With pyc_res_generator.py simply being:
#! /usr/bin/env python
# -*- coding: utf8 *-*
import argparse
parser = argparse.ArgumentParser(description="", formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-o", type=str, help="Output file name")
args = parser.parse_args()
with open(args.o, 'w') as output_file:
output_file.write( "// This is valid C++!" )
To test this, I write some invalid C++ in generated.cpp (like fjkfkfk). When I compile (target is Android), I see in the log:
echo Generating... && python.exe pyc_res_generator.py -o generated/generated.cpp && echo Generated!
Generating...
B:\Android\android-ndk-r11b/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-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 -g -g -marm -O0 -fno-omit-frame-pointer -Wall -Wno-psabi -W -D_REENTRANT -fPIC -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I..\TestQt -I. -IB:\QtCreator5_6_1\5.6\android_armv7\include -IB:\QtCreator5_6_1\5.6\android_armv7\include\QtWidgets -IB:\QtCreator5_6_1\5.6\android_armv7\include\QtGui -IB:\QtCreator5_6_1\5.6\android_armv7\include\QtCore -I. -isystem B:\Android\android-ndk-r11b\sources\cxx-stl\gnu-libstdc++\4.9\include -isystem B:\Android\android-ndk-r11b\sources\cxx-stl\gnu-libstdc++\4.9\libs\armeabi-v7a\include -isystem B:\Android\android-ndk-r11b\platforms\android-9\arch-arm\usr\include -IB:\QtCreator5_6_1\5.6\android_armv7\mkspecs\android-g++ -o main.obj ..\TestQt\main.cpp
B:\Android\android-ndk-r11b/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-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 -g -g -marm -O0 -fno-omit-frame-pointer -Wall -Wno-psabi -W -D_REENTRANT -fPIC -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I..\TestQt -I. -IB:\QtCreator5_6_1\5.6\android_armv7\include -IB:\QtCreator5_6_1\5.6\android_armv7\include\QtWidgets -IB:\QtCreator5_6_1\5.6\android_armv7\include\QtGui -IB:\QtCreator5_6_1\5.6\android_armv7\include\QtCore -I. -isystem B:\Android\android-ndk-r11b\sources\cxx-stl\gnu-libstdc++\4.9\include -isystem B:\Android\android-ndk-r11b\sources\cxx-stl\gnu-libstdc++\4.9\libs\armeabi-v7a\include -isystem B:\Android\android-ndk-r11b\platforms\android-9\arch-arm\usr\include -IB:\QtCreator5_6_1\5.6\android_armv7\mkspecs\android-g++ -o dialog.obj ..\TestQt\dialog.cpp
B:\Android\android-ndk-r11b/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-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 -g -g -marm -O0 -fno-omit-frame-pointer -Wall -Wno-psabi -W -D_REENTRANT -fPIC -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I..\TestQt -I. -IB:\QtCreator5_6_1\5.6\android_armv7\include -IB:\QtCreator5_6_1\5.6\android_armv7\include\QtWidgets -IB:\QtCreator5_6_1\5.6\android_armv7\include\QtGui -IB:\QtCreator5_6_1\5.6\android_armv7\include\QtCore -I. -isystem B:\Android\android-ndk-r11b\sources\cxx-stl\gnu-libstdc++\4.9\include -isystem B:\Android\android-ndk-r11b\sources\cxx-stl\gnu-libstdc++\4.9\libs\armeabi-v7a\include -isystem B:\Android\android-ndk-r11b\platforms\android-9\arch-arm\usr\include -IB:\QtCreator5_6_1\5.6\android_armv7\mkspecs\android-g++ -o generated.obj ..\TestQt\generated\generated.cpp
B:\QtCreator5_6_1\5.6\android_armv7\bin\moc.exe -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -IB:/QtCreator5_6_1/5.6/android_armv7/mkspecs/android-g++ -IC:/Users/jp225611/Documents/TestQt -IB:/QtCreator5_6_1/5.6/android_armv7/include -IB:/QtCreator5_6_1/5.6/android_armv7/include/QtWidgets -IB:/QtCreator5_6_1/5.6/android_armv7/include/QtGui -IB:/QtCreator5_6_1/5.6/android_armv7/include/QtCore -I. -IB:\Android\android-ndk-r11b/sources/cxx-stl/gnu-libstdc++/4.9/include -IB:\Android\android-ndk-r11b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include -IB:\Android\android-ndk-r11b/platforms/android-9/arch-arm//usr/include ..\TestQt\dialog.h -o moc_dialog.cpp
..\TestQt\generated\generated.cpp:1:1: error: 'fjkfkfk' does not name a type
// This is valid C++!
^
makefile:670: recipe for target 'generated.obj' failed
mingw32-make: *** [generated.obj] Error 1
mingw32-make: *** Waiting for unfinished jobs....
Generated!
13:59:08: Le processus "B:\QtCreator5_6_1\Tools\mingw492_32\bin\mingw32-make.exe" s'est terminé avec le code 2.
Erreur lors de la compilation/déploiement du projet TestQt (kit : android_armeabi-v7a)
When executing step "Make"
13:59:08: Temps écoulé : 00:03.
And I see that generated\generated.cpp was correctly generated by pyc_res_generator.py (it now contains // This is valid C++!)...but apparently too lately as the compiler saw fjkfkfk...
As you can see, the output reports:
Generating...
B:\Android\android-ndk-r11b/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++ .... ..\TestQt\generated\generated.cpp
Generated...
While one would expect:
Generating...
Generated...
B:\Android\android-ndk-r11b/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++ .... ..\TestQt\generated\generated.cpp
This only occurs when parallel builds are enabled (I set MAKEFLAGS environment variable to '-j8`)
It looks like my custom command generated_file is started before generated\generated.cpp is compiled, but the system does not wait for it to end before actually compiling generated\generated.cpp.
Am I doing something wrong?
PRE_TARGETDEPS (custom targets) is not the good approach here as generated_file.target must match exactly the Makefile target name which may be vary on different options:
shadow build enabled or not
host platform (using slashs or backslash as a folder separator)
probably targetted compiler/platform
However, custom compiler apparently works much better. That's how moccing files is done. A custom compiler can generate a new file to be compiled (and then the system waits for the file to be generated before compiling it, even when performing parallel builds).
# Set fuiles to be generated in a variable
TO_GENERATE = $$PWD/generated/generated.cpp
# Specify custom command output file:
custom_generator.output = $$PWD/generated/generated.cpp
# Specify custom command:
custom_generator.commands = 'echo "Generating..." && python $$PWD/pyc_res_generator.py -o $$PWD/generated/generated.cpp && echo "Generated..."'
# dependency:
custom_generator.depends = FORCE
# link to input file variable
custom_generator.input = TO_GENERATE
# link to variable to store generated file to
custom_generator.variable_out = SOURCES
# add to qmake:
QMAKE_EXTRA_COMPILERS += custom_generator
Then, you don't even need to specify SOURCES += $$PWD/generated/generated.cpp. And also, $$PWD/generated/generated.cpp gets deleted upon clean!
This works perfectly as expected!
You need to set the name of the target to the name of the file that it generates. Here's a full project file that works for me (qmake 3.1, Qt 5.10.1):
# Filename relative to the *build* directory.
generated_file.target = generated/generated.cpp
generated_file.commands = 'echo "Generating..." && mkdir -p generated && echo "int main() {}" > generated/generated.cpp'
generated_file.depends = FORCE
QMAKE_EXTRA_TARGETS += generated_file
# Filename relative to the *source* directory.
SOURCES += $$OUT_PWD/generated/generated.cpp
The reason is that generated_file.target becomes the name of the target in the Makefile, so when generated.cpp is needed, your custom rule will be executed to create the file.
You can also remove it from PRE_TARGETDEPS, because you're already adding the generated file to SOURCES instead, which creates the proper dependency.
Note that this approach generates the file in the build directory, not the source directory. This is why you need to specify $$OUT_PWD when adding it to SOURCES: by default, qmake seems to assume that SOURCES are relative to the source directory.
If you want to generate it in the source directory instead, you might think that you just prefix $$PWD everywhere, but then qmake sometimes uses absolute paths and sometimes relative, and everything fails. You can probably work around that with $$absolute_path($$PWD) but I haven't tried.
Relevant sections from the generated Makefile (comments are mine):
# How to create generated/generated.cpp.
# Depending on FORCE to make it always stale (fails if you create a file named FORCE :)).
generated/generated.cpp: FORCE
echo "Generating..." && mkdir -p generated && echo "int main() {}" > generated/generated.cpp
# generated.o depends on generated/generated.cpp.
# Note that it's not in the same directory; we don't care.
generated.o: generated/generated.cpp
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o generated.o generated/generated.cpp
# The final executable depends on the compiled generated.o.
OBJECTS = generated.o
$(TARGET): $(OBJECTS)
$(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS)
The qmake documentation is pretty vague about this, only stating that target should be "The name of the custom build target.". However, the example shows that they're setting it to the name of the output file. (As a bonus, it also shows how to not repeat that name in the commands.)
An alternative relies on the TEMPLATE=subdirs, to honour libname.depends in a parallel build environment. You would need two .pro files in each application folder. And one in the shared library folder. Any other folders in your development area that use TEMPLATE=subdirs need to be made compatible.
eg TestApp using LibLarry.
Folder TestApp has
bld.pro and TestApp.pro.
TestApp.pro:
#This is a TEMPLATE=subdirs, which gmake will interpret;
# making sure the build dependencies are respected
TEMPLATE = subdirs
SUBDIRS += bld mylib
bld.file = bld.pro
bld.depends = mylib # force library to be built first.
mylib.file = $${PATH_TO_LIB_LARRY}/liblarry.pro
bld.pro:
TARGET=TestApp
SOURCES+=TestApp.cpp
PRE_TARGETDEPS+=$${PATH_TO_LIB_LARRY}/liblarry.a
#boilerplate...
QT -= gui
CONFIG = ...
include ($${PATH_TO_LIB_LARRY}/liblarry.pri) # contains include file info
CONFIG += link_prl # allow lib larry to specify its position in the linker input command.
Folder LibLarry has
LibLarry.pro
containing
LibLarry.pro:
TARGET=larry
TEMPLATE=lib
CONFIG+=staticlib
CONFIG+=create_prl # export information, so apps can link correctly
SOURCES+=my_shared_function.cpp
include (liblarry.pri)
liblarry.pri:
HEADERS += $$PWD/my_shared_function.h
INCLUDEPATH += $$PWD

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.

Qmake doesn't use stdc++11 compilation flag

When I try to compile my project, I've got this error :
g++ -c -pipe -std=c++11 -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic -std=gnu++98 -Wno-deprecated -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/lib64/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I+= -I../includes -I. -I. -o ThreadPool.o ../sources/ThreadPool.cpp
In file included from /usr/include/c++/5.3.1/thread:35:0,
from ../includes/ThreadPool.hpp:4,
from ../sources/ThreadPool.cpp:1:
/usr/include/c++/5.3.1/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support \
I don't understand why because I use the flag -std=c++11 in my Makefile
#-------------------------------------------------
#
# Project created by QtCreator 2016-04-11T18:51:06
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = plazza_gui
SOURCES += main.cpp\
../sources/Window.cpp \
../sources/ThreadPool.cpp \
../sources/ProcessManager.cpp \
../sources/Process.cpp \
../sources/NamedPipe.cpp \
../sources/Xor.cpp \
../sources/AEncryptionMethod.cpp \
../sources/Cesar.cpp \
../sources/ProcessData.cpp \
../sources/Parser.cpp
HEADERS += ../includes/Window.hpp
INCLUDEPATH += += ../includes/
FORMS += forms/mainwindow.ui
QMAKE_CXXFLAGS += -std=c++11
LIBS += \
-lboost_regex \
DISTFILES += \
ressources/pizzera.jpg \
ressources/pizzeria.png \
ressources/icon.png
RESOURCES += \
ressources/ico.qrc
The Makefile works fine in my friend's computer, but when I clone it I can't compile it.
That means your compiler does not support c++11 in that way. Try this if you are sure your compiler is not that old:
CONFIG += c++11