QMake和pkg-config

时间:2019-06-24 10:03:06

标签: qmake pkg-config

我在我的Qt应用程序之一中使用libqtermwidget。碰巧的是,在该库的0.8.0版本中,引入了一些新功能,而这些功能在0.6.0版本中是不存在的。

由于libqtermwidget不提供任何版本宏,所以我想使用pkg-config在qmake中检查其版本,如下所示:

# i would like a functionality like this
if pkg-config --version qtermwidget5 < 0.8.0
    DEFINES += OLD_QTERMWIDGET

这当然可以在cpp文件中使用:

#ifndef OLD_QTERMWIDGET
    ... code for 0.8.0 and higher ...
#endif

1 个答案:

答案 0 :(得分:1)

您应该使用$$system()来调用pkg-config并读取stdout(如果有)。但是让我们以一种“通用”的方式对其进行编程:

# finds package version by invoking 'pkg-config'
# $$1 = package
# note: stores value in cache (stash) file for subsequent use
defineReplace(findPackage) {
    # using <package>Version variable
    pkg = $${1}Version
    !defined($$pkg, var) {
        # cache miss
        # note: $$pkgConfigExecutable() is an undocumented function from qt_functions.prf
        $$pkg = $$system($$pkgConfigExecutable() --modversion $$1)
        # cannot store the empty value
        isEmpty($$pkg): $$pkg = 0
        # save to the stash file
        cache($$pkg, stash)
    }
    # return value of <package>Version
    return($$eval($$pkg))
}


# now using this...
qtw5 = $$findPackage(qtermwidget5)
equals(qtw5, 0) {
    error("qtermwidget5 is not installed")
} else:!versionAtLeast(qtw5, 0.8.0) {
    warning("Found an old version of qtermwidget5 ($$qtw5)")
    DEFINES += OLD_QTERMWIDGET
} else {
    # nothing
}