Including .pro Files: Difference between revisions
(Some cleanup) |
(In the example, DEPENDPATH was wrongly typed as DEPENDSPATH) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{LangSwitch}} | {{LangSwitch}} | ||
[[Category:Tools]] | [[Category:Tools]] | ||
Line 5: | Line 4: | ||
QMake's project files sometimes need to rely on the include feature. This is a great tool, but there are some tricks of the trade to be aware of. | QMake's project files sometimes need to rely on the include feature. This is a great tool, but there are some tricks of the trade to be aware of. | ||
First up, a convention, pro-files meant for inclusion in other pro-files are commonly named '''.pri, just to indicate that they are for inclusion. This also means that qmake does not find them, but uses the appropriate pro-file instead. | First up, a convention, pro-files meant for inclusion in other pro-files are commonly named '''.pri, just to indicate that they are for inclusion. This also means that qmake does not find them, but uses the appropriate pro-file instead.''' | ||
In a pro-file, you have two important variables: INCLUDEPATH and DEPENDPATH. The first is used by the C++ compiler when resolving #include statements, while the latter is used by qmake when trying to determine what to build in which order. | In a pro-file, you have two important variables: INCLUDEPATH and DEPENDPATH. The first is used by the C++ compiler when resolving #include statements, while the latter is used by qmake when trying to determine what to build in which order. | ||
Line 11: | Line 10: | ||
To create a truly movable source tree, the pri-files update these variables appropriately. My trick to do that is to rely on the current working directory. You find that by running the $$system(pwd) command (on Unix/Linux only - sorry). | To create a truly movable source tree, the pri-files update these variables appropriately. My trick to do that is to rely on the current working directory. You find that by running the $$system(pwd) command (on Unix/Linux only - sorry). | ||
Within the [ | Within the [https://doc.qt.io/qt-5/qmake-variable-reference.html qmake variable reference] , there are variables that may help find the current working directory (Tested on Windows ). A couple of these are: | ||
''' [ | ''' [https://doc.qt.io/qt-5/qmake-variable-reference.html#pwd '''PWD''']''' | ||
'''''' | '''''' Specifies the full path leading to the directory containing the current file being parsed.''''' | ||
* [ | * [https://doc.qt.io/qt-5/qmake-variable-reference.html#out-pwd '''OUT_PWD'''] | ||
'''''' | '''''' Specifies the full path leading to the directory where qmake places the generated Makefile.''''' | ||
* | * The usage of the $$ prefix is detailed [https://doc.qt.io/qt-5/qmake-language.html#variable-expansion here]. | ||
When having | When having set up the include and depend on paths, it is just a matter of adding to the SOURCES, HEADERS, RESOURCES and FORMS sections. | ||
To summarize, here is a small example: | To summarize, here is a small example: | ||
<code>INCLUDEPATH | <code>INCLUDEPATH += $$system(pwd)/include | ||
DEPENDPATH += $$system(pwd) | |||
SOURCES | SOURCES += src/foo.cpp | ||
HEADERS | HEADERS += include/foo.h | ||
FORMS | FORMS += forms/foo.ui | ||
RESOURCES | RESOURCES += foo.qrc | ||
</code> | </code> | ||
Latest revision as of 06:07, 19 October 2020
QMake's project files sometimes need to rely on the include feature. This is a great tool, but there are some tricks of the trade to be aware of.
First up, a convention, pro-files meant for inclusion in other pro-files are commonly named .pri, just to indicate that they are for inclusion. This also means that qmake does not find them, but uses the appropriate pro-file instead.
In a pro-file, you have two important variables: INCLUDEPATH and DEPENDPATH. The first is used by the C++ compiler when resolving #include statements, while the latter is used by qmake when trying to determine what to build in which order.
To create a truly movable source tree, the pri-files update these variables appropriately. My trick to do that is to rely on the current working directory. You find that by running the $$system(pwd) command (on Unix/Linux only - sorry).
Within the qmake variable reference , there are variables that may help find the current working directory (Tested on Windows ). A couple of these are: PWD ' Specifies the full path leading to the directory containing the current file being parsed.
' Specifies the full path leading to the directory where qmake places the generated Makefile.
- The usage of the $$ prefix is detailed here.
When having set up the include and depend on paths, it is just a matter of adding to the SOURCES, HEADERS, RESOURCES and FORMS sections.
To summarize, here is a small example:
INCLUDEPATH += $$system(pwd)/include
DEPENDPATH += $$system(pwd)
SOURCES += src/foo.cpp
HEADERS += include/foo.h
FORMS += forms/foo.ui
RESOURCES += foo.qrc
Finally, in the pro-file, simply add the pri-file by calling include:
…
include(support/foo/foo.pri)
…