Usually the version is defined as a composition of several #define
values (like in the arv library for example).
So let's go for a simple and working example:
// myversion.h
#define __MY_MAJOR__ 1
#define __MY_MINOR__ 8
Then in your Makefile:
# Makefile
source_file := myversion.h
MAJOR_Identifier := __MY_MAJOR__
MINOR_Identifier := __MY_MINOR__
MAJOR := `cat $(source_file) | tr -s ' ' | grep "\#define $(MAJOR_Identifier)" | cut -d" " -f 3`
MINOR := `cat $(source_file) | tr -s ' ' | grep '\#define $(MINOR_Identifier)' | cut -d" " -f 3`
all:
@echo "From the Makefile we extract: MAJOR=$(MAJOR) MINOR=$(MINOR)"
Explanation
Here I have used several tools so it is more robust:
tr -s ' '
: to remove extra space between elements,
grep
: to select the unique line matching our purpose,
cut -d" " -f 3
: to extract the third element of the selected line which is the target value!
Note that the define values can be anything (not only numeric).
Beware to use :=
(not =
) see: https://stackoverflow.com/a/10081105/4716013