Qt: *.pro vs *.pri - qt

What is the difference between *.pro and *.pri configuration files for qmake?
What should go into a *.pro file and what should go into a *.pri file?

There is one main difference between their targetted reuse:
.pro
This is usually called Project File.
.pri
This is usually called Project Include File.
As you can see in their names, the main difference is that .pri files are meant to be include files. That is similar to including modules in programming language to share the functionality, essentially.
You will be able to write the common settings and code into those .pri files and include them from several .pro files as the need arises. This is how you would use it in practice:
foo.pri
FOO = BAR
hello.pro
...
include($$PWD/foo.pri)
...
world.pro
...
include($$PWD/foo.pri)
...
This way, the commonality would be available both in hello.pro as well as world.pro. It does not make much of difference in this scenario, but when the shared functionality gets longer, it will save you some writing as well as sync'ing, bugfixing, and so on.
You could even include a .pri file inside another .pri file if you wish. You could also include .pri files in different subprojects, etc. It is very nice.
The syntax is the same, however, for both the .pro and .pri files. In the end, you would run qmake on the .pro files, and that is also what qmake generates for you if you do not have a project file existing and you intend to use qmake -project.
You can read more about the include function in here:
include(filename)
Includes the contents of the file specified by filename into the current project at the point where it is included. This function succeeds if filename is included; otherwise it fails. The included file is processed immediately.
You can check whether the file was included by using this function as the condition for a scope.
Just to be complete, there are also .prf Project Feature Files and .prl Project Linker Files, but as an end user, you do not need to deal with that for now.

A .pro file is what you would run QMake on. A .pri file is included by a .pro file. Other than that there is not much of a difference between the two.
Example usage could be if you have different builds which need different options. You could put shared information in the .pro, while deferring the options to various .pri files. A bit more information, although admittedly not much more, can be found here.

The format of the .pri files is exactly the same as the format of the .pro files. The main difference is one of intent; a .pro is what most people would expect to run qmake on directly, while a .pri is to be included by a .pro. When you instruct qmake to include another file, it just processes the commands in that file as if it were in the current file.
For Reference: *.pro vs *.pri

Related

make dist with qmake

I have a Qt project with qmake.
After I generate the makefile, if I run make dist, the generated tarball will contain a bunch of .pri and .conf files that aren't part of my project but come from Qt. Is there a way to exclude these?
In any case shipping them with my sources is useless because they aren't sufficient to compile it.

In Qt, when should you use RESOURCES vs. DISTFILES vs. OTHER_FILES

Within the .pro file, things are straightforward if you're including header files (use HEADERS), C++ files (use SOURCES) and Objective-C / Objective-C++ files (use OBJECTIVE_SOURCES), but it is less clear to me how other file types should be included.
e.g. looking at various examples that Qt provide, there is inconsistency about whether QML files should be DISTFILES, OTHER_FILES or contained within a .qrc file (i.e. RESOURCES). I've gone with putting QML files in a qml.qrc file.
My question has arisen because I'm including data files, such as audio .wav files, within a .qrc file (also as shown in Qt examples; e.g. Qt Quick Demo - Maroon in Trouble) but this has slowed compiling down to a crawl. And in the case of MinGW on Windows, it simply crashes with an out of memory error when it reaches 1GB. There must be a better way to include these!
Could you provide a guide as to when to use:
DISTFILES
OTHER_FILES
RESOURCES
Quoting from the qmake manual:
RESOURCES
Specifies the name of the resource collection files (qrc) for the target. [...]
DISTFILES
Specifies a list of files to be included in the dist target. This feature is supported by UnixMake specs only. [...]
OTHER_FILES
This seems in fact undocumented, at least I could not find anything. As far as I can tell all files listed here are only linked in project explorer (of Qt Creator for example) and are not treated in any way by qmake.
As for your example you might consider shipping the files as they are and not include them in any resource file, because they are compiled into the binary.
You may also take a look at compiling external resources and using them for your large files to keep the simplicity of Qt's resource system.
DISTFILES: Something special for unix you won't use in most cases. From the docs:
Specifies a list of files to be included in the dist target. This
feature is supported by UnixMake specs only.
OTHER_FILES: Files, that are part of you project, but not of the "build". This can be things like a readme, build hints, or any other stuff, that does not fit into any other categories
RESOURCES: .qrc-files, that will be compiled into the application.
Regarding the usage of those three with QML:
You can basicly use DISTFILES or OTHER_FILES for other files. In QtCreator the appear in a node as other files. These two are exchangeable for most developers. The Qt examples are local project, thus either they don't require a resource or have both, i.e. you can find the QML-files in for example OTHER_FILES and RESOURCES.
For QML files, you should always use RESOURCES, to make sure they are within your binary.
Speaking of DISTFILES , you can try it yourself.
Assuming you are using Qt on Windows. Here is a very simple qmake project file project.pro:
TEMPLATE = app
DISTFILES += "C:\Windows\explorer.exe"
qmake it, and you can find the following statements in generated Makefile.Debug and Makefile.Release:
DIST = C:\Windows\explorer.exe
...
dist:
$(ZIP) project.zip $(SOURCES) $(DIST) project.pro
...
The target dist is one of the standard targets. I learned it from makefile - What is the difference between make and make dist? - Stack Overflow.

Making moc skip files/folders during build

It is a known bug that moc trips over macros used in libstdc++ as documented here: http://lists.kde.org/?l=necessitas-devel&m=132317657926916&q=raw
I am trying to compile a project which uses gcc 4.6.3 and am stuck because moc trips over the macros.
One way to overcome the problem is to include the directives as mentioned in this link:
http://doc.qt.io/qt-4.8/moc.html
but that is time consuming and not a very clean way as every file has to have these directives.
What I'd like to know is, can qmake be configured such as to make moc skip certain directories/files?
edited: typos
According to the Qt qmake docs moc will be run for files that are added to the HEADERS variable (emphasis mine):
qmake will generate dependency information (unless -nodepend is specified on the command line) for the specified headers. qmake will also automatically detect if moc is required by the classes in these headers, and add the appropriate dependencies and files to the project for generating and linking the moc files.
So if you don't want moc to be run for certain files then don't add them to the HEADERS in your .pro file. However, for some platforms that might cause the headers not to be found when compiling the corresponding .cpp files. To fix that, add an INCLUDEPATH for the folders containing such headers - moc won't be run for headers that are inside an INCLUDEPATH.

Does the qmake includes pri files automatically in the pro file

Assume the following directory structure
project/project.pro
project/project.pri
In this case even if the project.pro file does not have a statement like (include project.pri). Does it gets included automatically by qmake.
In the qtcreator sources plugin directory
qt-creator/src/plugins/coreplugin
There is a coreplugin.pri file which is not included in the coreplugin.pro file
I'm fairly confident that it does not get included automatically, and I did a quick test with a .pro and a .pri file that had message calls in them to confirm this.
With regards to the files you're seeing in creator, it's not uncommon to have a .pri file that is designed for other .pro files to include so that you can use the project.

Using .pri files in Qt

This is a followup to this question How to create a subdirectory for a project in qt-creator?, where the first answer didn't work for me.
I resolved it by manually writing every file into the main .pro file, which is not that much harder, but I still wonder - how exactly .pri files work, and why the solution linked above didn't add the folders, but only the .pri files, so it looked like this in Qt creator:
So, my questions are:
What is the general format of the .pri files?
Why the solution above doesn't work?
The format of the .pri files is exactly the same as the format of the .pro files. The main difference is one of intent; a .pro is what most people would expect to run qmake on directly, while a .pri is to be included by a .pro. When you instruct qmake to include another file, it just processes the commands in that file as if it were in the current file.
The last time I looked at Qt Creator, it would do additional parsing and recognize that .pri files were separate, and list any headers/sources/ui/rc/pri files from that file specifically as a subdirectory, much like you see the include.pri files listed in the screenshot of this question.
My guess from looking at your screenshot is that QtCreator doesn't find the header files listed in the .pri file. If your .pri file is located in a different directory than your .pro file (which seems to be the case here), Qt looks for the files listed in the .pri file in the same directory as the .pri file (not the .pro file), and any relative path will be resolved from that directory.
When a file can't be found, QtCreator silently ignores it and simply doesn't add it to the folder in the projects view. If, for example, you used the full path for line.h, circle.h and bezier.h in your include.pri file, as soon as you save the file, you'll see them appear in the projects view. The key now is simply to figure out what is the appropriate relative path pointing to those files relative to the .pri file.
Extracted from Qt 4.5: Managing Projects:
The .pri file contains the list of source files, header files, .ui files, and .qrc files in the project.
For more about these files and their OSs:
A .vcproj file containing Windows-specific settings and listing the files in the project.
A .pro file containing Unix and/or Mac OS X specific settings.
A .pri file (a qmake include file) listing the files in the project.
I suggest you to take a look on the link.

Resources