qmake:如果存在 pkg-config 中的标志,则使用它们,否则使用默认值

qmake: Use flags from pkg-config if they exist, otherwise use defaults

在我的设置中,我想同时支持系统范围的 Qt 安装和自定义 Qt 安装。我可以使用 pkg-config 为我的系统范围安装获取正确的编译和 link 标志:

CONFIG += link_pkgconfig
PKGCONFIG += Qt5Core

但是,如果 pkg-config 找不到 Qt5Core,构建将失败并显示 Project ERROR: Qt5Core development package not found

我不想失败,而是想设置合理的默认值(例如 /usr/local/qt5)。它应该实现以下目标:

if pkg-config can find Qt5Core {
    PKGCONFIG += Qt5Core
} else {
    INCLUDEPATH += /usr/local/qt5/
    LIBS += -lQt5Core
}

如何在我的项目配置中完成此操作?

您可以使用 system 内置的测试功能 executes the given command in a secondary shell. Succeeds if the command returns with a zero exit status; otherwise fails.

system(pkg-config --exists Qt5Core) {
    PKGCONFIG += Qt5Core
} else {
    INCLUDEPATH += /usr/local/qt5/
    LIBS += -lQt5Core
}

为此有一个 qmake 函数: http://doc.qt.io/qt-5/qmake-test-function-reference.html#packagesexist-packages

packagesExist(Qt5Core) {
    PKGCONFIG += Qt5Core
} else {
    INCLUDEPATH += /usr/local/qt5/
    LIBS += -lQt5Core
}