11

What are the possibilities to know at compile time the time and date (BUILDTIME) in order to include in the binaries (executable/libraries) the information about the moment in which they have been created, in a portable way?

We have currently a solution that uses sh.exe, and requires to install msys under Windows, but I was wondering if it was possible doing without it.

Searching things like "build time/date", "compile time/date" did not lead to any relevant result.

Edit:

When I got to know about __TIME__, it was then possible to find this question had previously been asked: Recording the time when you compile a source

Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197

4 Answers4

13

The standard macros __DATE__ and __TIME__ do the job.

Be careful that this will provide you the compilation date of the file where they are used. Not the link date. Thus, you have to touch the file each time it is build, or do a pre-build step in MSVC.

The C99 standard says:

6.10.8 Predefined macro names

The following macro names shall be defined by the implementation:

  • __DATE__: The date of translation of the preprocessing translation unit: a character string literal of the form "Mmm dd yyyy", where the names of the months are the same as those generated by the asctime function, and the first character of dd is a space character if the value is less than 10. If the date of translation is not available, an implementation-defined valid date shall be supplied.
  • __TIME__: The time of translation of the preprocessing translation unit: a character string literal of the form "hh:mm:ss" as in the time generated by the asctime function. If the time of translation is not available, an implementation-defined valid time shall be supplied.

I copied the C99 text here, but these macros are much older than C99... I did not manage to find the standard text for older C...

Antonio
  • 19,451
  • 13
  • 99
  • 197
Matthieu Rouget
  • 3,289
  • 18
  • 23
  • Not only GCC or MVS support these macro's, but any ANSI C compiler. In practice you can assume that all C/C++ compilers support these. – meaning-matters Jun 25 '13 at 08:52
  • @meaning-matters and Matthieu: Would it be possible to reference that? This is the closest I could find: http://c0x.coding-guidelines.com/6.10.8.html – Antonio Jun 25 '13 at 09:40
  • @Antonio: coding-guidelines.com does not look to be more _official_ (regarding the C standard) than the GCC/MSDN documentation I copied in my post. I updated my post with a reference to the latest ISO standard (publicly available). – Matthieu Rouget Jun 25 '13 at 15:16
1

You could use macros like __DATE__ and __TIME__. This is also portable for Visual Studio (see http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80).aspx)

JeffRSon
  • 10,404
  • 4
  • 26
  • 51
1

You could always write a 5-line program that calls time and strftime and outputs that to a file, rather than using sh. [I have used this approach for updating "build numbers" and/or "version number" in the past].

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
1

In addition to the __DATE__ and __TIME__ macros you can pass the value to your program at build time using a macro.

In your Makefile you can pass the date at build time in a macro:

gcc -Wall -DBUILD_TIME=`date +%d%m%y_%T` tst.c -o tst

(or use any other date format)

then stringify in your code:

#define STR(x) STR1(x)
#define STR1(x) #x

printf("%s\n", STR(BUILD_TIME));  // display build time
ouah
  • 142,963
  • 15
  • 272
  • 331
  • Nice one, but I don't think is portable to Windows/MinGW, is it? – Antonio Jun 25 '13 at 09:00
  • build>gcc -Wall -DBUILD_TIME=`date +%d%m%y_%T` tst.c -o tst gcc: +%d%m%y_%T`: No such file or directory . . . . . . . tst.c:4:8: error: expected declaration specifiers or '...' before string constant . . . . . . . . . tst.c:4:16: error: expected declaration specifiers or '...' before string constant. . . . . . . Can it be you have something else installed? How do I get a new line in the comment box?!? :) – Antonio Jun 25 '13 at 09:27
  • @Antonio You need to put the backquotes to enclose the `date` command as in my example. – ouah Jun 25 '13 at 09:37
  • They are there, but they have been "eaten" by the formatting... If I put backquotes in the comments, `it becomes like this` – Antonio Jun 25 '13 at 09:39
  • @Antonio I initially put `STR(DBUILD_TIME)` instead of `STR(BUILD_TIME)`, I think it was the cause of your error. – ouah Jun 25 '13 at 09:47
  • No, I am afraid that was not the problem – Antonio Jun 25 '13 at 11:26