Qt qmake .pro files use built-in variables instead of absolute paths

created at 05-31-2022 views: 5

environment

  • Win10
  • CMake 3.23.0-rc2
  • Qt Creator 6.0.2 Based on Qt 6.2.2 (MSVC 2019, 64 bit)

foreword

Sometimes we need to refer to some header files that come with Qt. For example, when using quazip, you can directly use the QtZlib that comes with Qt. At this time, you may directly add the following sentence to xxx.pro:

INCLUDEPATH += D:\Qt\6.2.4\msvc2019_64\include\QtZlib

However, due to the use of absolute paths, your project will be less portable. So we can solve this problem by using Qmake built-in environment variables.

Qmake built-in environment variables

Please read the above official documents by yourself first, the meaning and usage of each variable will not be redundantly described here.

For the above example, we can get D:\Qt\6.2.4\msvc2019_64 through the variable QT_INSTALL_PREFIX, and test it:

C:\Users\fygame>d:

D:\>cd D:\Qt\6.2.4\msvc2019_64\bin

D:\Qt\6.2.4\msvc2019_64\bin>qmake -query "QT_INSTALL_PREFIX"
D:/Qt/6.2.4/msvc2019_64

Then the above configuration can be directly modified to:

INCLUDEPATH += $$[QT_INSTALL_PREFIX]\include\QtZlib

Is this OK? Unfortunately, on Windows, you may still not find the relevant header files at compile time, why?

Note the difference between slashes and backslashes

Let's take a look at the difference between the compilation instructions generated by these two methods

use absolute path

cl -c -nologo -Zc:wchar_t -FS -Zc:rvalueCast -Zc:inline -Zc:strictStrings -Zc:throwingNew -permissive- -Zc:__cplusplus -Zc:externConstexpr -Zi -MDd -std:c++17 -utf-8 -W3 -w34100 -w34189 -w44996 -w44456 -w44457 -w44458 -wd4577 -wd4467 -EHsc /Fd.obj\qztest.vc.pdb -DUNICODE -D_UNICODE -DWIN32 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -DWIN64 -DNOMINMAX -DQT_QML_DEBUG -DQT_NETWORK_LIB -DQT_CORE5COMPAT_LIB -DQT_TESTLIB_LIB -DQT_CORE_LIB -DQT_TESTCASE_BUILDDIR="\"F:/tgit/qtprojects/build-quazip-0.7.3_Qt_6_2_4_MSVC2019_64bit-Debug/qztest\"" -I..\..\quazip-0.7.3-qt6\qztest -I. -I..\..\quazip-0.7.3-qt6\qztest -ID:\Qt\6.2.4\msvc2019_64\include\QtZlib -I..\..\quazip-0.7.3-qt6 -ID:\Qt\6.2.4\msvc2019_64\include -ID:\Qt\6.2.4\msvc2019_64\include\QtNetwork -ID:\Qt\6.2.4\msvc2019_64\include\QtCore5Compat -ID:\Qt\6.2.4\msvc2019_64\include\QtTest -ID:\Qt\6.2.4\msvc2019_64\include\QtCore -I.moc -ID:\Qt\6.2.4\msvc2019_64\mkspecs\win32-msvc -Fo.obj\ @C:\Users\fygame\AppData\Local\Temp\qztest.obj.355020.15.jom

Note that the option for QtZlib is -ID:\Qt\6.2.4\msvc2019_64\include\QtZlib

use built-in variables

cl -c -nologo -Zc:wchar_t -FS -Zc:rvalueCast -Zc:inline -Zc:strictStrings -Zc:throwingNew -permissive- -Zc:__cplusplus -Zc:externConstexpr -Zi -MDd -std:c++17 -utf-8 -W3 -w34100 -w34189 -w44996 -w44456 -w44457 -w44458 -wd4577 -wd4467 -EHsc /Fd.obj\qztest.vc.pdb -DUNICODE -D_UNICODE -DWIN32 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -DWIN64 -DNOMINMAX -DQT_QML_DEBUG -DQT_NETWORK_LIB -DQT_CORE5COMPAT_LIB -DQT_TESTLIB_LIB -DQT_CORE_LIB -DQT_TESTCASE_BUILDDIR="\"F:/tgit/qtprojects/build-quazip-0.7.3_Qt_6_2_4_MSVC2019_64bit-Debug/qztest\"" -I..\..\quazip-0.7.3-qt6\qztest -I. -I..\..\quazip-0.7.3-qt6\qztest -ID:/Qt/6.2.4/msvc2019_64/include/QtZlib -I..\..\quazip-0.7.3-qt6 -ID:\Qt\6.2.4\msvc2019_64\include -ID:\Qt\6.2.4\msvc2019_64\include\QtNetwork -ID:\Qt\6.2.4\msvc2019_64\include\QtCore5Compat -ID:\Qt\6.2.4\msvc2019_64\include\QtTest -ID:\Qt\6.2.4\msvc2019_64\include\QtCore -I.moc -ID:\Qt\6.2.4\msvc2019_64\mkspecs\win32-msvc -Fo.obj\ @C:\Users\fygame\AppData\Local\Temp\qztest.obj.360680.31.jom

Note that the options for QtZlib are - ID:/Qt/6.2.4/msvc2019_64/include/QtZlib

conclusion

The only difference between the two is the difference between slashes and backslashes. Windows paths use backslashes, while built-in variables use slashes. So, modify the above configuration statement again:

INCLUDEPATH += $$system_path($$[QT_INSTALL_PREFIX])\include\QtZlib

Replace the slashes with backslashes and you can compile~

A note about the qmake function:

created at:05-31-2022
edited at: 05-31-2022: