Paste timestamp in executable name using makefile

I have a simple make file and I want to insert the current date and time into the executable that it creates.

Something like: NOW=$(date +"%c") will be added to the exe name. What is the best way to do this?

thanks!

+4
source share
1 answer

I suppose you already have a Makefile that creates the application. So here you can add:

 # Use ':=' instead of '=' to avoid multiple evaluation of NOW. # Substitute problematic characters with underscore using tr, # make doesn't like spaces and ':' in filenames. NOW := $(shell date +"%c" | tr ' :' '__') # Main target - your app + "date" all: foo_$(NOW) # Normal taget for your app which already have. foo: foo.cpp # Copy "normal" app to app_DATE # You'll rater want copy then move, otherwise make will have # to link your app again during each execution (unless that's # exactly what you want). foo_$(NOW): foo cp $^ $@ 

Note the replacement of ':' by '_' . As stated here , if the date contains a colon, it probably won’t be able to parse the Makefile.

I don’t have access to Mac OS X at the moment, so this was only tested on Ubuntu, but I worked on the Mac once and I didn’t notice any significant differences in make . So this will work for you too.

--- change ---

As Beta correctly comments, the method described above creates a new copy with the current date each time make is called. Sometimes this may be desirable, so I will leave it and offer the following alternative for situations where this is not the case:

 # Same as above... NOW := $(shell date +"%c" | tr ' :' '__') # Default target all: foo # <-- not foo_$(NOW) anymore, foo_$(NOW) target is removed altogether OBJ := foo.o bar.o # other ... # Normal taget for your app which already have, but... foo: $(OBJ) $(CXX) $(LDFLAGS) $^ -o $@ cp $@ $@ _$(NOW) # <-- additional copy at the end (read on below) 

Why did the target foo_$(NOW) disappear? Since you only want to create a copy of the application with a date check if you modified the application directly. This means that you cannot create a target, because then make always created a copy (as in the above scenario).

This, however, means that make not aware of the existence of a copy. A copy is missing from the dependency graph that make creates at startup. Therefore, a copy cannot be used as a prerequisite for any other purpose. This is not a flaw, but a direct result of what we do not know in advance if we are going to create a copy or not. (If anyone has a way to overcome this without performing a minor run, please treat me :)).

+7
source

Source: https://habr.com/ru/post/1434658/


All Articles