QT multiple project configuration - c++

New to QT but can't see how I can't switch between debug and release configurations across multiple projects.
I have a session with 20+ projects in it, and switching between 4 configurations means going into each one and selecting a different config, which is just a different flag passed to the makefile. This is far too time consuming, is there a quicker way?

You can make a Subdirs project and add your subprojects to its .pro file :
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += \
project1 \
project2 \
project3 \
...
Here the project directories should be added in the Subdirs project directory alongside its .pro file. Notice that the name of the .pro file of the subprojects should be the same as it's folder name. This way the subprojects are detected and listed in the Projects pane automatically.

You can create a new project like foobar.pro:
TARGET = foobar
TEMPLATE = subdirs
SUBDIRS += ../foo \
../bar
This will enable you to compile foo.pro and bar.pro on release/debug mode with one command.

Related

Qt Creator (not CMake) how to specify different files for different build configurations?

I have project in QT Creator (as .pro file, not as CMake). Now I want to add tests (gtest) as another build configuration, so I've added build configuration "debug_tests" (Projects -> Add -> Debug with name debug_tests).
Now I want to add specific main file for "debug_tests" and exclude from build my normal main file when the build configuration is choosen.
So I've tried:
test_debug:{
message("Running tests:")
SOURCES += \
tests/MultiCellArticleModelTests.cpp \
tests/main_tests.cpp
LIBS += \
-lgtest
}
But it is not working, it is working when I do like that:
!debug_and_release:{
message("Running tests:")
SOURCES += \
tests/MultiCellArticleModelTests.cpp \
tests/main_tests.cpp
LIBS += \
-lgtest
}
But it is not good solution if I want to add more configurations.
Another solution which I see is to just add defines to compilation and have ifdef in my code.
Usually, tests are in their own sub-projects rather than part of some scope in your main source tree.
This allows you to keep them cleanly separated from your main code and you can run them easily. You can even call make check to run the whole test suite that you created for your library/application.

How to make makefile generated by Qt without qmake dependency

I use Qt creator in linux to make my non-Qt c++ project. I find that Qt creator would make a makefile for me. I want to move all the project into a computer without any qt or qmake, but I cannot really edit the makefile myself. As I google that someone says add a CONFIG -= qt flag can make a pure g++ makefile without any qt component but actually not.
the pro file in my project is like this:
QMAKE_LFLAGS += -no-pie
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
INCLUDEPATH += /home/MyName/opencvBuild/install/include/opencv4/
LIBS += -L/home/MyName/opencvBuild/install/lib/ \
-lopencv_core \
SOURCES += \
main.cpp \
helloopencv.cpp
HEADERS += \
helloopencv.hpp
and the makefile generate thousands of Qt dependencies like:
.....
###### Files
SOURCES = ../HelloOpenCV/main.cpp \
../HelloOpenCV/helloopencv.cpp
OBJECTS = main.o \
helloopencv.o
DIST = ../Qt/5.12.0/gcc_64/mkspecs/features/spec_pre.prf \
../Qt/5.12.0/gcc_64/mkspecs/common/unix.conf \
../Qt/5.12.0/gcc_64/mkspecs/common/linux.conf \
../Qt/5.12.0/gcc_64/mkspecs/common/sanitize.conf \
../Qt/5.12.0/gcc_64/mkspecs/common/gcc-base.conf \
.....
now when I call make command in terminal it would automatically link to qmake.
I don't want any "Qt" in my makefile. What should I do?
These are not "Qt"-dependencies, but rather "qmake"-dependencies: it's a list of files which qmake had processed in order to generate your Makefile. Stuff like gcc-base.conf is needed for some common gcc options, sanitize.conf for a bunch of -fsanitize= options, and so on.
Thus, it's the list of the files your Makefile itself depends on (used for auto-regeneration and such). Of course, if you don't intend to ever regenerate Makefile with the help of qmake, you can just delete all these lines at once.
You complain that despite of having CONFIG-=qt in your .pro, there is still a bunch of qt_config.prf and other such files mentioned in that list. This is true, however qmake startup scripts designed precisely in this way: first, all Qt-related stuff is unconditionally preconfigured; then the user project is processed; and then, only if CONFIG += qt, the relevant Qt stuff finally becomes enabled.
Just for fun, you can mess with qmake startup code: go to <prefix>/share/qt5/mkspecs/linux-g++-64 (or whatever your QMAKE_SPEC is); open the file qmake.conf; comment out the last string: #load(qt_config). Now your CONFIG -= qt project should be processed okay, but the resulting Makefile will be significantly smaller. But the price is that qmake cannot do Qt-enabled projects anymore (well, in fact, you can add load(qt_config) on top of your .pro file and it may even work - no warranties of any kind, of course ;-).

Build targets with different templates in Qt

Hi I am relatively new to Qt and I could not find a viable solution to this so far. I have the following project structure
ALPHA-app
alpha.pro
ALPHA-Desktop
alpha-desktop.pro
ALPHA-Shared
alpha-shared.pro
ALPHA-Common
alpha-common.pro
ALPHA-Tests
alpha-tests.pro
I basically have to run alpha.pro which builds all the projects and creates an executable
alpha.pro,
TEMPLATE = subdirs
DESTDIR = .
SUBDIRS += \
ALPHA-Common \
ALPHA-Shared \
ALPHA-Desktop \
ALPHA-Tests
ALPHA-Desktop.depends = ALPHA-Common
ALPHA-Shared.depends = ALPHA-Common
ALPHA-Tests.depends = ALPHA-Common
ALPHA-Tests.depends = ALPHA-Desktop
the main executable that will be run is present in the ALPHA-Desktop project (given below)
TARGET = ALPHA #this is in ALPHA-Deskop.pro
TEMPLATE = app
Now I created ALPHA-Tests to make a project to run unit-tests. It depends on ALPHA-Desktop (Hence I wrote the .depends statement in the ALPHA project)
To run the ALPHA-Tests, I need to create a static library of the ALPHA-Desktop folder. Hence I wrote the following lines in ALPHA-Desktop.pro file
TARGET = ALPHA-Desktop #section 1
TEMPLATE = lib #section 1
CONFIG += staticlib #section 1
TARGET = ALPHA #section 2
TEMPLATE = app #section 2
When I run the ALPHA.pro file, the ALPHA-Desktop library is not getting created. Only the ALPHA executable is being created. If I interchange the positions (i.e., put section 2 above section 1) then only the libALPHA-Desktop.a file is created and the executable is not created.
How do I solve this? (I am using qmake version 3.0, Qt version 5.4.2 on ubuntu 15.10)
TEMPLATE, TARGET etc are always global for one .pro file. While this is never explicitly spelled out in the qmake documentation, it is obvious once you realize that the qmake syntax aims to be declarative. The last line setting a variable will always prevail (except if you overwrite the variable via the command line).
If you want to create a static library and an executable, you need to have two .pro files (probably again integrated with a subdirs .pro file).
Note though that this doesn't necessary mean that you need different subdirectories, since multiple .pro files can also exist in the same directory:
ALPHA-Desktop.pro:
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += ALPHA-Desktop-staticlib.pro ALPHA-Desktop-app.pro
ALPHA-Desktop-staticlib.pro:
TEMPLATE = lib
CONFIG += static
# ...
ALPHA-Desktop-app.pro:
TEMPLATE = app
# ...

Include a qmake project in another qmake project

I have created an application in qt/c++
Browser/Browser.pro
To start the browser, I need an agent. I have created an agent and store it in the Browser folder
Browser/Browser.pro
Browser.cpp
main.cpp
Agent/Agent.pro
Agent/agent.cpp
When building the Browser, I'm generating an app but it's only build the browser.
I have added in Browser.pro the line below:
SUBDIRS += \
Agent/Agent.pro
browser.depend = Agent/Agent.pro
My concerns is that I'm trying to build browser.app and automatically agent.app .
The goal after is to integrate the the agent.app generated in the resource of the browser.app
Any idea
SUBDIRS variable is only interpreted in TEMPLATE=subdirs pro file.
Also, it does take only the directory name where to find your project, not the .pro itself.
You should create a directory for your Browser project and get this:
Browser/
|-Browser.pro
|-Browser/
|-Browser.pro
|- ...
|-Agent/
|-Agent.pro
|- ...
And then your main project file will be like this:
Browser.pro:
TEMPLATE = subdirs
SUBDIRS = Agent Browser
CONFIG += ordered
More information about subdirs : http://doc.qt.io/qt-5/qmake-variable-reference.html#subdirs

qt build output directory being totally ignored

i've been using qt recently and was organizing some projects into a Subdirs to begin a larger project, and went to manipulate the pro files so as to place my build files where i want them. However, the changes i make to any of them are being ignored. I have a main project that compiles to an executable, and then two other projects that compile to DLL's. I would like for the executables and libraries to end up in the same directory, where the subdirs pro file is located. Here is the pro file for my subdirs that i've tried:
TEMPLATE = subdirs
SUBDIRS += \
MainWindow \
FileLoader
Debug:DESTDIR = $$_PRO_FILE_/debug
Debug:OBJECTS_DIR = $${DESTDIR}/.obj
Debug:MOC_DIR = $${DESTDIR}/.moc
Debug:RCC_DIR = $${DESTDIR}/.rcc
Debug:UI_DIR = $${DESTDIR}/.ui
Release:DESTDIR = $$_PRO_FILE_/release
Release:OBJECTS_DIR = $${DESTDIR}/.obj
Release:MOC_DIR = $${DESTDIR}/.moc
Release:RCC_DIR = $${DESTDIR}/.rcc
Release:UI_DIR = $${DESTDIR}/.ui
After that, i put that same path block in all three of the subprojects pro files, and still all the build files end up in a directory in my Qt Projects directory called "build-Chromatic-Desktop_Qt_5_1_1_MSVC2010_32bit-Debug" (Chromatic is the name of the project). Can someone help me wrangle my pro files to do make this stuff where i want it? Again, i want all the build files for all projects in the directory where the subdirs pro file is.
Instead of using _PRO_FILE_ you should use _PRO_FILE_PWD_ because the first variable contains the path to the project file in use and the second contains the path to the directory containing the project file in use.
In Qt Creator you should deselect the Shadow build checkbox: "By default, Qt Creator builds projects in a separate directory from the source directory, as shadow builds. This keeps the files generated for each build and run kit separate. If you only build and run with a single kit, you can deselect the Shadow build checkbox."