0

Scenario:
I am have a Qt App with a QtApp.pro some C++ code in main.cpp. To keep this question simple, please assume that it is a blank QtQuick2 app created from the QtCreator Application wizard.

Question:
Is it possible to run some Qt commands in the .pro files or C++ code in main.cpp?

What if I want to run this git command git rev-parse HEAD from my QtApp.pro or main.cpp and embed the commit ID somewhere on my UI to show the commit ID of the release? Is it possible? How?

Environment:
The Qt version I am running is Qt 5.9.3
Operating System : MacOS High Sierra

Mike
  • 8,055
  • 1
  • 30
  • 44
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • 1
    You don't want to run `git` commands *from* your Qt App, but just when *building* that Qt App. BTW, your question could be operating system specific: on Linux, a `.pro` file generates a `Makefile`, and `make` can run any command specified in the `Makefile` – Basile Starynkevitch May 18 '18 at 07:56
  • I am running macos high sierra. updated my question with the same info. Is it possible to embed the command in pro file and access the pro file variable into an `std::string`? – TheWaterProgrammer May 18 '18 at 08:00
  • See the answer here https://stackoverflow.com/a/66315563/827880 – rightaway717 Feb 22 '21 at 12:25

2 Answers2

3

Is it possible to embed the command in pro file and access the pro file variable into an std::string?

To run command in pro file, this post, Running a program/script from QMake, shows you how to run script in .pro. I think you can write your git command in that script.

To access the pro file variable into an std::string. you can transfer the file variable by -D option of gcc, which you can define it with QMAKE_CFLAGS in .pro file. this post, How to define a string literal in gcc command line? shows you how to use -D option.

gzh
  • 3,507
  • 2
  • 19
  • 23
  • 1
    @Game_Of_Threads, By the way, I guess you are trying to do something like this one, [qmake pre-build step before ANY compilation ](https://stackoverflow.com/questions/15864689/qmake-pre-build-step-before-any-compilation) . :-) – gzh May 18 '18 at 08:34
0

You need to think separately of build time (includes compile-time) and run time. In general (think of some cross-compilation scenario, or just of a Qt application deployed on other computers) that could happen on different machines and on different days.

At run time, you could use 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 with their source code tree).

At build time, on Unix-like machines, a .pro Qt project file is generating a Makefile which is used by make for build. That Makefile could contain git commands. See _timestamp.c target of this Makefile (not using Qt so hand-written, not generated by qmake) as an example; it contains details about the current git commit.

How to configure qmake to generate custom commands in the Makefile is a different question; you might add custom targets.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • could you cite and example using the `QProcess` way? the `QProcess` way of running a git command looks like an easier approach – TheWaterProgrammer May 18 '18 at 08:15
  • Did you read the documentation of [QProcess](http://doc.qt.io/qt-5/qprocess.html) ? But I insist: by default the source files are not available, because you would deploy your Qt application on other machines without transmitting its source files. – Basile Starynkevitch May 18 '18 at 08:15