How to add pre-build step in qmake/qtcreator?

 ̄綄美尐妖づ 提交于 2019-11-30 13:04:49

问题


I want the compiled application to have the commit number, source files checksums and other things to be available during the compilation.

In plain Makefiles I do like this:

prog: VERSION source.c
    gcc -DVERSION=\"$(shell cat VERSION)\" source.c -o prog 

VERSION: .git
    git describe > VERSION

How to use something similar with qmake?


回答1:


If you were to pass the version information as an included file (let's say "version.h") instead of a #define, then you could add the following to your qmake file

# Define how to create version.h
version.target = version.h
version.commands = <PUT_YOUR_COMMANDS_HERE>
version.depends = .git

QMAKE_EXTRA_TARGETS += version

PRE_TARGETDEPS += version.h

The first 3 lines tell how to make a new target object called "version" that generates "version.h". It is made by executing the commands "<PUT_YOUR_COMMANDS_HERE>". The target is dependent on ".git"

The "QMAKE_EXTRA_TARGETS" says there is a new target known as "version".

The "PRE_TARGETDEPS" indicates that "version.h" needs to exist before anything else can be done (which forces it to be made if it isn't already made).




回答2:


A simpler solution even if @jwernemy as nice way to solve it:

VERSION = $$system(-git-dir=$PWD/.git <PUT_YOUR_GIT_COMMANDS_HERE>)


来源:https://stackoverflow.com/questions/5083441/how-to-add-pre-build-step-in-qmake-qtcreator

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