2

I am developing a program in C that needs to include the date and the time of the last push performed in a given branch in the git repository in order to version it.

I know how I can get the data from git and use it to generate a version number in the format I want. (https://stackoverflow.com/a/51403241/7114946)

The problem is I don't know how to include this within the code of the program because it ultimately needs to "printf" this info during the starting of the app.

I have found the preprocessor macros __TIME__ and __DATE__. I wonder if there are other similar macros to include some given string/data.

Thanks

edit: I need to include this info in compilation time, not when the code is uploaded into git.

Daniel Ortega
  • 425
  • 5
  • 11

1 Answers1

3

You can define macros on the command line:

gcc -DFOO=BAR -c prog.c

acts like #define FOO BAR in prog.c.

You can combine this with shell command interpolation to get the output of an external program in there:

gcc -DGIT_TIMESTAMP="\"$(git whateveryourcommandis)\"" prog.c

(The escaped quotes are there to make the macro expand to a string literal.)

And then prog.c can do e.g.:

printf("my git timestamp is %s\n", GIT_TIMESTAMP);
melpomene
  • 84,125
  • 8
  • 85
  • 148