构建 Qt 应用程序时如何 运行 git 命令
How to run git commands when building a Qt App
场景:
我在 main.cpp
中有一个带有 QtApp.pro
一些 C++ 代码的 Qt 应用程序。为了让这个问题简单化,请假设它是一个从 QtCreator 应用程序向导创建的空白 QtQuick2 应用程序。
问题:
是否可以 运行 .pro
文件中的某些 Qt 命令或 main.cpp
中的 C++ 代码?
如果我想从我的 QtApp.pro
或 main.cpp
运行 这个 git 命令 git rev-parse HEAD
并将提交 ID 嵌入到我的 UI 来显示发布的提交 ID?可能吗?怎么样?
环境:
Qt版本我运行ning是Qt 5.9.3
操作系统:MacOS High Sierra
您需要单独考虑 build time (includes compile-time) and run time. In general (think of some cross-compilation scenario, or just of a Qt application deployed 在其他计算机上)可能在不同机器和不同日期发生的情况。
在 运行 时,您可以将 QProcess to run some git
command (or whatever program or script you need to run). Of course, you need git
to be installed, and perhaps your source code tree to be available at run time (on a deployed and installed Qt application, that might not be the case by default; for example most Qt applications on my Linux distribution are not installed 与他们的源代码树一起使用。
在构建时,在类 Unix 机器上,一个 .pro
Qt 项目文件正在生成一个 Makefile
which is used by make
for build. That Makefile
could contain git
commands. See _timestamp.c
target of this Makefile
(不是使用 Qt 这么手写的,不是由 qmake
生成的)举个例子;它包含有关当前 git 提交的详细信息。
如何配置qmake
to generate custom commands in the Makefile
is a different question; you might add custom targets.
Is it possible to embed the command in pro file and access the pro file variable into an std::string?
到 pro 文件中的 运行 命令,此 post、Running a program/script from QMake 向您展示如何 运行 在 .pro 中编写脚本。我认为您可以在该脚本中编写 git 命令。
将 pro 文件变量访问 std::string。
你可以通过gcc的-D
选项来传递文件变量,你可以在.pro文件中用QMAKE_CFLAGS定义它。 post、How to define a string literal in gcc command line? 向您展示了如何使用 -D
选项。
场景:
我在 main.cpp
中有一个带有 QtApp.pro
一些 C++ 代码的 Qt 应用程序。为了让这个问题简单化,请假设它是一个从 QtCreator 应用程序向导创建的空白 QtQuick2 应用程序。
问题:
是否可以 运行 .pro
文件中的某些 Qt 命令或 main.cpp
中的 C++ 代码?
如果我想从我的 QtApp.pro
或 main.cpp
运行 这个 git 命令 git rev-parse HEAD
并将提交 ID 嵌入到我的 UI 来显示发布的提交 ID?可能吗?怎么样?
环境:
Qt版本我运行ning是Qt 5.9.3
操作系统:MacOS High Sierra
您需要单独考虑 build time (includes compile-time) and run time. In general (think of some cross-compilation scenario, or just of a Qt application deployed 在其他计算机上)可能在不同机器和不同日期发生的情况。
在 运行 时,您可以将 QProcess to run some git
command (or whatever program or script you need to run). Of course, you need git
to be installed, and perhaps your source code tree to be available at run time (on a deployed and installed Qt application, that might not be the case by default; for example most Qt applications on my Linux distribution are not installed 与他们的源代码树一起使用。
在构建时,在类 Unix 机器上,一个 .pro
Qt 项目文件正在生成一个 Makefile
which is used by make
for build. That Makefile
could contain git
commands. See _timestamp.c
target of this Makefile
(不是使用 Qt 这么手写的,不是由 qmake
生成的)举个例子;它包含有关当前 git 提交的详细信息。
如何配置qmake
to generate custom commands in the Makefile
is a different question; you might add custom targets.
Is it possible to embed the command in pro file and access the pro file variable into an std::string?
到 pro 文件中的 运行 命令,此 post、Running a program/script from QMake 向您展示如何 运行 在 .pro 中编写脚本。我认为您可以在该脚本中编写 git 命令。
将 pro 文件变量访问 std::string。
你可以通过gcc的-D
选项来传递文件变量,你可以在.pro文件中用QMAKE_CFLAGS定义它。 post、How to define a string literal in gcc command line? 向您展示了如何使用 -D
选项。