5

using c++

I compile my code on an automated schedule and need to use the time at which the code was compiled in the code itself. Currently I'm just using the __DATE__, __TIME__ macros to get the compile- time date and time. However, this causes the binaries to change even if no changes have been made to the source (macros will inflate at compile time) which is not good (i don't want the setup to think that the binary changed if there have been no changes to the source).

Is it possible to get the compile-time without using any means that would cause the source to change?

Thanks

5
  • Besides saving it in an additional file that is not part of the build, I don't think that's possible...
    – Nbr44
    Jul 12, 2013 at 10:31
  • 2
    In that case you might want to make your build-system to only build if there are any changes. Recompiling just for sake of recompilation when there are no changes to any sources doesn't make much sense. Jul 12, 2013 at 10:32
  • 2
    (Note: You should edit your post and add backticks around the macros names: "... the `__DATE__`, `__TIME__` macros ...", because currently they appear bold and without the underscores)
    – gx_
    Jul 12, 2013 at 10:32
  • I like both answers ;) Jul 12, 2013 at 10:32
  • so you want the date and time of the point of compilation, but you dont want your binaries to change based on that information? so the only way to do that is to ignore that information and make sure it isnt used in anything that gets stored to the binary...
    – PlasmaHH
    Jul 12, 2013 at 10:33

3 Answers 3

3

The standard __DATE__ and __TIME__ macros do what you observe, return a time dependent string.

It depends upon the system (and perhaps the compiler) and notably the build system (like GNU make for example).

A possible idea could be to link in a seperate timestamp file, something like (in make syntax)

timestamp.c:
        date +'const char timestamp[]="%c";' > $@

program: $(OBJECTS) timestamp.c
        $(LINKER.cc) $^ -o $@ $(LIBES)
        rm -f timestamp.c

The timestamp.owould then be regenerated and your programwould be relinked at every make (so the generated program will indeed change, but most of the code -thru $(OBJECTS) make variable- will stay unchanged).


Alternatively, you could e.g. log inside some database or textual log file the time of linking, e.g.

program: $(OBJECTS)
      $(LINKER.cc) $^ -o $@ $(LIBES)
      date +'$@ built at %c' >> /var/log/build.log

(you might use logger instead of date to get that logged in the syslog)

Then the generated program won't change, but you'll have logged somewhere a build timestamp. BTW you could log also some checksum (e.g. $(shell md5sum program) in make syntax) of your binary program.

3
  • Hmm, wouldn't this still cause the resulting binary file to change from build to build? Since the timestamp.c file will also change and the timestamp.0 file will be linked into the binary? Jul 12, 2013 at 11:22
  • I need to make it so that the final binaries do not change unless there has been an actual change in source. Jul 12, 2013 at 11:23
  • @user1322488 Take his first makefile snippet, but then modify the timestamp.c line by adding a dependency. e.g. timestamp.c: $(OBJECTS) This will make it only regenerate timestamp.c when it's older than any of the objects.
    – Dave S
    Jul 12, 2013 at 11:59
0

If you use the compile-time IN YOUR binaries, then you will have the binary change.

There are several solutions, but I think the main point is that if you rebuild the binaries on a regular basis, it should really only be done if there are actually some changes (either to the build system or to the source code). So make it part of your build system to check if there are changes, and don't build anything if there isn't any changes. A simple way to do this is to check what the "latest version" in the version control system for the source code is. If the latest version is the same as the one used in the previous build, then nothing needs to be built. This will save you generating builds that are identical (apart from build time-stamp), and will resolve the issue of storgin __DATE__ and __TIME__ in the binary.

0

It's not clear to me what you want. If it's the last modified time of the file, getting it will depend on your system and build system: something like -D $(shell ls -l --time-style=long-iso $< | awk '{ print $7, $8 }') could be used in the compiler invocation with GNU make under Linux, for example. But of course, it means that if an include file was changed, but not the source, the time and date wouldn't reflect it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.