QtCreator tricky project - qt

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.

Related

Could not find .pro file for sub dir - when subdir contains relative path

I've got a top-level .pro file with subdirs template
TEMPLATE = subdirs
SUBDIRS += \
common/tests
common/tests contains .pro file, but qmake couldn't find it:
Could not find .pro file for sub dir "common/tests" in "/home/o.antonyan/Documents/mt3/main/src/common/tests"
But it's there:
ls /home/o.antonyan/Documents/mt3/main/src/common/tests
api interfaces Makefile tests_common.pro.user ui
dataformat.cpp local_enviroment.cpp tests_common.pro types
If I move this pro file to parent directory and modify topp-level qmake SUBDIRS += common then it works. The problem is only when subdirs contain relative path more than 1 level deep.
QMake version 3.0 Using Qt version 5.4.1 on GNU/Linux x86, Qt compiled from sources
From the docs:
It is recommended that the project file in each subdirectory has the same base name as the subdirectory itself, because that makes it possible to omit the file name. For example, if the subdirectory is called myapp, the project file in that directory should be called myapp.pro.
Try renaming tests_common.pro to tests.pro.
I had the same problem and found this solution for the top level .pro file:
TEMPLATE = subdirs
tests_common.file = tests/tests_common.pro
SUBDIRS += tests_common
But now I would prefere the answer of svlasov.

If you include a .pri, shouldn't that .pri's include path affect your #includes?

If I include(someOtherProject.pri) in my .pro, and that other project file (.pri) has INCLUDEPATH += . in it, then shouldn't my files in my .pro find those headers when I #include them? I cleaned and ran qmake on everything, regenerated the .pri file by simply copying the .pro from that other project and renaming it with the .pri extension, but when I #include a file from that other project, compiler says it cannot find it.
I wouldn't expect that I must also edit the INCLUDEPATH of my working .pro file if I am already using include(someOtherProject.pri) -- that would make the point of using include(someOtherProject.pri) unnecessary altogether.
The solution is to have INCLUDE PATH += $$PWD/ in the .pri instead, as this will expand out to the proper hard-coded path no matter where it is included.

The right way to use a custom library in Qt?

I have a custom library to be included in Qt. Currently, it is included by adding the followings in .pro file.
INCLUDEPATH += ...
DEPENDPATH += ...
LIB += ...
Since the library is used in most of my projects. I am wondering that if there is some way to make my custom library to be INTEGRATED with the QtSDK? Maybe it can be included by syntax like the build-in components
QT += my_custom_library
and everything (header file include path, lib file include, etc...) will be done.
Is the integration possible?
You are probably looking for to create a mkspec file which then used with CONFIG option. So create a file to $QT_HOME/mkspec/features/mycoollib.prf that contains your instructions. There should be plenty of examples in that directory. The name of the file declares it's usage, so the file mycoollib.prf would be used like:
CONFIG += mycoollib

Is there any special category for external files in project file of Qt Creator?

I'd like to keep in my project some external files, let's say .txt ones.
Is there any special category for external files in project file (.pro) of Qt Creator?
Qt Creator, at least, uses an OTHER_FILES variable for .txt files and anything similar:
OTHER_FILES += test.txt
Adds "test.txt" to the "Project Files" pane in Qt Creator.
If you want to group your files a bit, what you can try is adding a separate .pri file such as "External.pri", including your external files from there, and then including the .pri file in your .pro:
Project.pro
HEADERS += someclass.h
SOURCES += main.cpp someclass.cpp
...
include(External.pri)
External.pri
OTHER_FILES += license.txt todo.txt
Which has the effect of creating a folder of sorts (called "External") in Qt Creator. If you have a bunch of external files it'll help keep things organized.

How to use QMake's subdirs template?

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.

Resources