QMake and pkg-config

十年热恋 提交于 2019-12-11 21:12:45

问题


I am using libqtermwidget in one of my Qt applications. It so happens that in version 0.8.0 of the library, some new features have been introduced, which are absent in 0.6.0 version.

Since libqtermwidget does not provide any version macros, I would like to use pkg-config to check its version, something like this, in qmake:

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

This of course could be used inside the cpp file:

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

回答1:


You should use $$system() to invoke pkg-config and to read stdout (if any). But let's program it in a bit more "generic" way:

# 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
}


来源:https://stackoverflow.com/questions/56734224/qmake-and-pkg-config

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!