Auto time/date compiled

Jul 30, 2008 at 7:13pm
Hey guys,

Is there a smart way to add a date/time string to my program so when it starts up it can say this has been compiled on (date) and (time).

I know version numbers would probably be the more appropriate way to go but I am not disciplined enough to increment them all the time.

What I can do now is something like....

printf("\n\nStarting program created on %s at time %s\n",__DATE__,__TIME__);

However the problem is I would have to re-compile main.c each time via "touch" or whatever. Is there a better way to insert date/time via Makefile or do you have other thoughts?

Thanks!
Jul 30, 2008 at 8:54pm
Doing it this way, you could have the makefile write a header file or source file that then gets included in the executable. this file has a macro or function called by main() that does the above.

In my experience, this functionality is usually handled by the configuration tool/code repository being used. You embed a couple of variables in each source/header file in the code and/or comment blocks and as you take a copy out of the repository, it replaces these variables with time, date, file version etc. I'm currently setting up subversion at work to do just that.
Jul 30, 2008 at 8:59pm
The problem is I'm not that proficient at make files.

I think I can add into the "all" rule to "touch main.c or version.h" like you suggested right before compile time. Suggestions?

I make small little changes quite often and hand them to somebody to test on hardware. It would just be nice for something to happen when I type make that assures that something is getting incremented so I can know whether he has the latest version or not.

Thanks
Jul 30, 2008 at 9:55pm
If your main concern is just recompiling main.c then there is no need to bother too much. Just add it as a FORCE recompile and link each time. It won't cost any significant time.
1
2
3
4
5
6
7
8
9
foo: main.o ...
	...

main.o: main.c
	...

main.c:
	

Compiles my program 'foo'. The dependencies of 'foo' are 'main.o' etc. (Replace the elipses with your proper dependencies.) The next line instructs how to link everything.

The 'main.o' target depends on 'main.c'.

'main.c' has no dependencies and no commands, so it is always considered "modified". (But notice that lines 7 and 8 are still properly-formed.)

(You don't actually need to touch main.c, the __DATE__ and __TIME__ automacros are properly handled by the CPP no matter what the file date.)

Hope this helps.
Last edited on Jul 30, 2008 at 9:56pm
Jul 30, 2008 at 10:01pm
Yes, doing a touch should be fine it will force make to recompile. You can force make to recompile a file using dependencies but I'd need to see your makefile to see how to do that and it may not be simple.
Topic archived. No new replies allowed.