-1

This question already has an answer here:

I am trying to extract the compilation date from a linux command (or cpp would be fine too). I am using:

stat -c %z ./myProgram.bin

However, if I copy myProgram.bin to an another place via ssh for example, the stat command is basically giving me the date of the copy.

How can I get the real compilation date? Thanks.

marked as duplicate by Rui F Ribeiro, Jeff Schaller, don_crissti, Anthon, roaima May 19 '16 at 16:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

Thomas Dickey's answer addresses the issue in general, for any (ELF) binary. Given the way your question's phrased, you might find the __DATE__ and __TIME__ predefined macros useful; they allow the compilation date and time to be referred to within a program (so a program knows its own compilation date and time).

Here's a quick example:

#include <stdio.h>

int main(int argc, char **argv) {
    printf("This program was compiled on %s at %s.\n", __DATE__, __TIME__);
    return 0;
}
  • Perfect! It is exactly what I was looking for! I was using fp = popen(command, "r") to use the linux stat command inside my cpp progam. Thanks a lot – superours May 19 '16 at 13:02
  • Ok! I removed it and I put it directly on stackoverflow. Thank you. – superours May 19 '16 at 14:29
0

stat -c %z displays last creation/updating time of file and it's not changeable or preservable. Hence if you copy the file to other place, it will always get new now creation time. What are you looking for is stat -c %y, to display last modification time, which is usually preserved by most tools dealing with files and directories.

0

You can copy preserving the modification time, e.g.,

cp --preserve=timestamps source destination

or (more generally: mode, ownership and timestamps)

cp -p source destination

although in some cases, preserving ownership is not wanted.

Similarly, you can copy to remote systems using scp's -p option:

scp -p source remote:destination

but scp does not have separate options for the mode, etc. Since it runs with the given remote permissions, it does not have a feature for setting remote ownership at the same time. Because of these limitations, you might want to use tar to make an archive which you extract on the remote system, preserving as much information as possible.

The ELF format used for binary files does not include a date, so the file's modification timestamp is all that you can preserve.

Further reading:

  • cp isn't quite as useful when copying files "via ssh", but scp knows about -p too. – Stephen Kitt May 19 '16 at 12:19

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