0

Suppose I have a variable date which is defined with extern in source code, i.e, extern date; then I want to assign a value to it at link time getting time from the computer on which it is compiled and assign to date variable. Is there a way to do that for example in gcc?

eral
  • 123
  • 1
  • 16

2 Answers2

1

Are __TIME__ and __DATE__ what you are looking for ?

If compiling and linking is one step in your scenario you can have your compiler and linker replace those macros with the date and time. If you compile one day and link the other this will not work because the compiler (better: preprocessor) decides which value is inserted.

Have a look at this or other posts here on stackoverflow.

Community
  • 1
  • 1
Marged
  • 10,577
  • 10
  • 57
  • 99
  • 1
    Otherwise one can just pass a symbol definition using the `-D` option with a value formatted from external shell command like`date`. – Eugene Sh. Jun 26 '15 at 20:56
  • @EugeneSh. That is even easier. I misinterpreted the question and thought eral wants to be able to see the value in the binary (when doing a hexdump for example). In that case one would need another macro to turn the symbol into a string (char array) – Marged Jun 26 '15 at 21:01
  • I am not quite sure I understand what you mean. Both methods should give the same result. But in yours it will be done by the preprocessor defining the symbols by itself, in mine it will be done externally. But in both cases we will end up with symbols defining the current time – Eugene Sh. Jun 26 '15 at 21:08
  • @EugeneSh. Doesn't matter as I was on the wrong track ;-) But like you said: both approaches deliver the date, but your approach is more flexible – Marged Jun 26 '15 at 21:11
1

One could pipe a date variable with the output of the date command into gcc just prior to the link command, so the variable date contains the current date and time of linkage. The options for gcc to read the source code from pipe for C is -xc -.

/* hello.c */
#include <stdio.h>

int main(void)
{
    extern char const date[];

    printf("Hello, link date is %s\n", date);
    return 0;
}

$ gcc -c hello.c
$ echo "char const date[] =\"`date`\";" | gcc -c -xc - -o date.o;gcc hello.o date.o
$ ./a.out
Hello, link date is Sat Jun 27 11:59:19 CEST 2015
$ 
4566976
  • 2,419
  • 1
  • 10
  • 14
  • this also works, but simpler is more beautiful. Thanks though – eral Jun 27 '15 at 15:11
  • @eral This approach is for the circumstances which [Marged](http://stackoverflow.com/users/1354537/marged) has explained in his answer. If you compile one day and link the other this approach would work. – 4566976 Jun 28 '15 at 14:11
  • Yes, but not for my case, I just hit the button and IDE does everything in a minute. This means there is at most 1 minute between pre-processing and linking and it is ok . – eral Jun 28 '15 at 18:57
  • @eral Consider if the program consists of more than one file you get the preprocess time of the file containing the date variable. If you change another file and link the program again the date remains on the old value. – 4566976 Jul 02 '15 at 16:31
  • yes ur right. But adding touch argument as a prebuild step in order to always compile the file this macros are in, solves this issue. – eral Jul 03 '15 at 17:29