I have several questions about what to do to have the best organization project for my needs.
Context
I'm working on QT5 and I have a project like this:
MyProject /
MyProject.pro
src /
src.pro // target = app (include Controller.pro and View.pro)
Controller /
Controller.pro // no target, just add some sources
Component1 /
Component2 /
...
View /
View.pro // no target, just add some sources
Component10 /
Component11 /
...
main.cpp
test /
Controller /
Component1 /
Component1.pro // target = app
main.cpp
...
View /
Component10 /
Component10.pro // target = app
main.cpp
...
Compilation
At the moment, everything works fine except that, the more my project grows, the more the compilation time is huge. (Around 2mins).
I checked what happened and the problem is, in each test-sub-project-file, I include Controller.pro in order to have all of my sources.
Doing this, I compile N times my sources and that's why it's too long.
(In the generated files/folders, I have something like)
build-MyProject /
src /
main.o
component1.o
component2.o
component10.o
...
test /
Controller /
Component1 /
main.o
component1.o
...
View /
Component10 /
main.o
component10.o
...
Component1 and Component10 had been compiled twice.
What I would like
Obviously, I want to generate objects for each file once. Ideally, something like:
MyProject /
obj /
src /
Controller /
component1.o
component2.o
View /
component10.o
main.o
test /
Controller /
Component1 /
main.o
View /
Component10 /
main.o
What I don't know how to do
1- The main problem is, I don't know how to tell QMake i want to link ob/src/Controller/Component1.o with obj/test/Controller/main.o
2- According to your experience, is an organization like this, a good thing or too complicated, too much separated, ... ?
3- With qmake tool, i saw that you can define target = app or target = lib to generate either an executable or a library. Is there a key word to only generate object without performing the linking ? (I can do it taking off main.cpp in key word SOURCES but make tries to link all objects and I have undefined reference to main...)
Thanks in advance.
I think separation of code modules from each other as much as possible is a good idea. This way you can have independent software modules which are reusable and able to be changed easily.
I personally use Subdirs to separate parts of my code. Using it, the project becomes much cleaner and easier to read. Qt Creator also provides good automation in liking the parts to each other.
You can make a Subdirs project and add the subprojects to its .pro file :
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += \
Controller\
View\
Component1\
Component2\
component10\
test\
mainApp
You should bring the subprojects that others depend on, first in the list. Also notice that the name of the .pro file of the subproject should be the same as it's folder name. This way the subprojects are detected and listed in the Projects pane.
The subprojects like Controller and View could be libraries. Part of .pro file for Controller:
TARGET = Controller
TEMPLATE = lib
DEFINES += Controller_LIBRARY
SOURCES += ...
HEADERS += ...
The subprojects like mainApp and test should be app. Part of .pro file for mainApp :
TARGET = mainApp
TEMPLATE = app
You can use the libraries in each subproject by linking it to the subproject. This can be done by right clicking on the subproject and choosing "Add Library" and then "Internal Library". When you select one library from the list of subprojects, the linking configurations are added to the .pro automatically. It will be like :
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../Controller/release/ -lController
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../Controller/debug/ -lController
else:unix: LIBS += -L$$OUT_PWD/../Controller/ -lController
INCLUDEPATH += $$PWD/../Controller
DEPENDPATH += $$PWD/../Controller
Related
I have a QML plugin project and my application project plus subdirs project organized like that:
project.pro
plugin/
plugin.pro
Plugin.cpp
Plugin.h
qmldir
app/
app.pro
main.cpp
main.qml
qml.qrc
project.pro:
TEMPLATE = subdirs
SUBDIRS = app plugin
app.depends = plugin
plugin.pro:
TEMPLATE = lib
QT += qml quick
CONFIG += plugin c++11
TARGET = $$qtLibraryTarget(plugin)
PLUGIN_URI = com.test.plugin
SOURCES += Plugin.cpp
HEADERS += Plugin.h
app.pro:
QT += qml quick
SOURCES += main.cpp
RESOURCES += qml.qrc
It builds as expected and now I need to pass path to compiled libplugin.dylib/.so/.dll and qmldir to my application. I see two ways:
Copy plugin files into /path/to/qt/5.10.1/clang_64/qml/com/test/plugin/ using INSTALL qmake variable.
Copy plugin files near the application executable and add this path as relative to the executable using QQmlEngine::addImportPath().
What approach will be better? How to do it?
I am building a Qt application which consists of several components (including plugins). Each sub project contains several classes.
I want to use the "normal" TDD workflow in my development process - namely;
I write a new function/method of a class in a module as a test
Compile the test (which should fail) for that module
Modify the source to correct the error (by adding the func/method etc)
Write test cases for the new function/method
Modify the src code to fix tests that fail
Each sub project will contain a src/ and test/ folders which will hold the source files and unittests respectively
This is what the project directory structure looks like:
myapp
|
|-myapp.pro
|
|--module1/
| |-src/
| |-test/
|
|--module2/
| |-src/
| |-test/
|
.
.
|--moduleN/
| |-src/
| |-test/
How can I setup QCreator so that I can build a subproject or its unittest from the QCreator GUI?
You can make a subdirs project and add the subprojects to its .pro file :
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += \
module1 \
module2 \
...
moduleN \
myapp
You should bring the subprojects that others depend on, first in the list. Also notice that the name of the .pro file of the subproject should be the same as it's folder name. This way the subprojects are detected and listed in the Projects pane.
The subprojects module1, module2 ... moduleN could be libraries which have TEMPLATE = lib in their .pro file and myapp should be app containing TEMPLATE = app in the .pro file.
You can use the libraries in each subproject by linking it to the subproject. This can be done by right clicking on the subproject and choosing Add Library and then Internal Library. When you select one library from the list of subprojects, the linking configurations are added to the .pro automatically. It will be like :
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../module1/release/ -lmodule1
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../module1/debug/ -lmodule1
else:unix: LIBS += -L$$OUT_PWD/../Base/ -lmodule1
INCLUDEPATH += $$PWD/../module1
DEPENDPATH += $$PWD/../module1
You should use subdirs template in .pro file.
I believe, you can start from following link (at the bottom of linked page).
I have a project in Qt Creator which has several shared library projects and the main project which also contains the main function. What I want is to add a new project which shouldn't be a shared library but just a project with some header files where I keep definitions and error codes. What I wish is to be able to add the path of this project to other projects INCLUDEPATH in order to use those files in other projects.
To do so I created an empty project which .pro file looks like this:
HEADERS += \
myHeader.h
but when I build the whole project it complains that it doesn't find the main in this project with only one header.
Is it possible in QtCreator to achieve this?
Create a .pri file which has your INCLUDEPATH, etc; and then refer to it in your other projects' .pro files:
# Common.pri
INCLUDEPATH += ../myPath
INCLUDE += myHeader.h
# OtherProject.pro
!include( ./Common.pri ) {
error( Could not find the Common.pri file. )
}
INCLUDEPATH += ../myOtherPath
Remember to use the += operator in your .pro files otherwise they will overwrite the .pri file variables.
I'm starting to learn Qt. I'm moving from the Visual Studio world and I am looking for a way to organize my project's structure using QMake. I've found the 'subdirs' template but I have quite a hard time understanding it.
My project structure looks like this:
project_dir/
main.cpp
project.pro
logic/
logic.pro
some logic files
gui/
gui.pro
gui files
My project.pro looks like this
TEMPLATE = subdirs
SUBDIRS = logic \
gui
SOURCES += main.cpp
In the .pro files for the subdirectories I have appropriate SOURCES, HEADERS and RESOURCES variables set.
Please tell me what TARGET, TEMPLATE and other necessary values I should set in the .pro files.
Also, is there some good QMake tutorial other than the official one?
In addition to Troubadour's comment, I would note that the SUBDIRS target is only good for specifying subdirectories. Therefore, your extra line of
SOURCES += main.cpp
in your project.pro file is incorrect, and will likely fail to build your main.cpp file, at worst. At best, qmake will refuse to parse the file, since it has conflicting specifications in it.
I've used the SUBDIRS template a few times, and it does well if you can build parts into more-or-less independent libraries, apparently like you have with the logic and the gui separate. Here is one way to do this:
project_dir/
-project.pro
-common.pri
-logic/
----logic.pro
----some logic files
-gui/
----gui.pro
----gui files
-build/
----build.pro
----main.cpp
project.pro:
TEMPLATE = subdirs
SUBDIRS = logic \
gui
# build must be last:
CONFIG += ordered
SUBDIRS += build
common.pri:
#Includes common configuration for all subdirectory .pro files.
INCLUDEPATH += . ..
WARNINGS += -Wall
TEMPLATE = lib
# The following keeps the generated files at least somewhat separate
# from the source files.
UI_DIR = uics
MOC_DIR = mocs
OBJECTS_DIR = objs
logic/logic.pro:
# Check if the config file exists
! include( ../common.pri ) {
error( "Couldn't find the common.pri file!" )
}
HEADERS += logic.h
SOURCES += logic.cpp
# By default, TARGET is the same as the directory, so it will make
# liblogic.a (in linux). Uncomment to override.
# TARGET = target
gui/gui.pro:
! include( ../common.pri ) {
error( "Couldn't find the common.pri file!" )
}
FORMS += gui.ui
HEADERS += gui.h
SOURCES += gui.cpp
# By default, TARGET is the same as the directory, so it will make
# libgui.a (in linux). Uncomment to override.
# TARGET = target
build/build.pro:
TEMPLATE = app
SOURCES += main.cpp
LIBS += -L../logic -L../gui -llogic -lgui
# Will build the final executable in the main project directory.
TARGET = ../project
You use subdirs if the logic and gui folders actually repesent some sort of target, eg. a library, that can be built independently of anything else. If that's the case then just use
TEMPLATE = lib
TARGET = logic
CONFIG += dll
in logic.pro.
If they are not independent targets but are just folders that exist to organise the sources files then you can just use a .pri file in each instead and include them within the .pro using
include(logic/logic.pri)
include(gui/gui.pri)
Just remember that the file paths in the .pri files are relative to the .pro file and not the .pri. BTW, the use of a .pri file is optional as you can still list the files in those folders directly in the .pro file. The .pri file just makes it that bit neater and helps keep the .pro file shorter.
I'm writing a small qt app suite that consists of a set of small programs that work on the same set of files. They are organized like this:
/
app1/
main.cpp
app2/
main.cpp
app3/
main.cpp
common/
project.h
project.cpp
somemore.h
somemore.cpp
appsuite.pro
When I do qmake && make, I want the following binaries to be built:
app1/app1
app2/app2
app3/app3
How do I write appsuite.pro to work like this?
I have heard something about .pri files, but I could not figure out how to use them in my "situation".
Help appreciated,
jrh
One way of doing it is to have a .pro file per subdirectory.
appsuite.pro:
TEMPLATE = subdirs
SUBDIRS = common app1 app2 app3
app1.depends = common
app2.depends = common
app3.depends = common
app1/app1.pro:
TARGET = app1
SOURCES = main.cpp
INCLUDEPATH += ../common
LIBS += -L../common -lcommon
The common.pro file should build a static library you can then link into the binaries.
common/common.pro:
TEMPLATE = lib
CONFIG = staticlib
SOURCES = project.cpp more.cpp
HEADERS = project.h more.h
One way is to create your global project appsuite.pro, like this:
TEMPLATE = subdirs
SUBDIRS = app1 app2 app3
The subprojects app1.pro and app2.pro should also be created for those applications alone, with a dependency regarding the common/ subdirectory
You can also specify other dependencies in appsuite.pro, for example if app1 depends on app2, as:
app1.depends = app2