-1

I want to be able to use a variable in my code that is the git version. If I set the variable with make and then override it in a .h file then the final value that prints wil lbe from the '.h' file. Is there a way to override the .h from the makefile?

 executing make
gcc -O2 -pipe   -pedantic -std=c99 -Wall -O3 -ledit -DVERSION=\"\" -c main.c -o main.o
In file included from main.c:9:0:
openshell.h:17:0: warning: "VERSION" redefined [enabled by default]
 #define VERSION "v0.1a"

I'm trying to set my variable VERSION from github so that a version can be viewed:

$ ./a.out --version
OpenShell version 0.1(a)
Version: v0.1a-2-gc6b1-dirty

I have this makefile

CC = gcc
GIT_VERSION := $(shell git describe --abbrev=4 --dirty --always --tags)
CFLAGS := $(CFLAGS) -pedantic -std=c99 -Wall -O3 -ledit -g -DVERSION=\"$(GIT_VERSION)\"

shell: main.o
    $(CC) -o shell main.o errors.c util.c pipeline.c -ledit

main.o: main.c errors.c util.c

.PHONY: clean
clean:
    rm -f *.o

Then it works on Linux Ubuntu but it doesn't work on BSD, with PC-BSD the variable is not shown:

dac:/usr/home/dac/openshell $ make
The Command is: make
27152: executing make
gcc -O2 -pipe   -pedantic -std=c99 -Wall -O3 -ledit -DVERSION=\"\" -c main.c -o main.o
main.c: In function 'trimstring':
main.c:116:9: warning: value computed is not used [-Wunused-value]
         *tmp++;
         ^
main.c:119:17: warning: comparison between pointer and integer [enabled by default]
     while (*tmp != NULL) {
                 ^
main.c: In function 'exec_program':
main.c:444:9: warning: implicit declaration of function 'snprintf' [-Wimplicit-function-declaration]
         snprintf(shell_prompt, sizeof(shell_prompt), "%s:%s $ ", getenv("USER"), getcwd(NULL, 1024));
         ^
main.c:444:9: warning: incompatible implicit declaration of built-in function 'snprintf' [enabled by default]
gcc -o shell main.o errors.c util.c pipeline.c -ledit
dac:/usr/home/dac/openshell $ ./shell --version
The Command is: ./shell --version
27181: executing ./shell
OpenShell version 0.1(a)
Version: 
dac:/usr/home/dac/openshell $ 

Update 160426 07:49 CET

Now it works with Ubuntu (but maybe dangerous deleting the old version number in the .h file) `GIT:= $(shell head -n -1 openshell.h > temp.txt ; mv temp.txt openshell.h;git describe --abbrev=4 --dirty --always --tags > VERSION; echo "#define VERSION \"$(GIT_VERSION)\"" >> openshell.h)

The updated Makefile is now:

CC = gcc
GIT_VERSION := $(shell git describe --abbrev=4 --dirty --always --tags)
CFLAGS := $(CFLAGS) -L/usr/local/include/ -L/usr/include -pedantic -std=c99 -Wall -O3 -g -DVERSION=\"$(GIT_VERSION)\" -ledit -lncurses

LDIRS = -L/usr/local/lib -L/usr/lib
LIBS = -ledit lncurses -lcurses

shell: main.o
    $(CC) -o shell main.o errors.c util.c pipeline.c -ledit -lncurses -lcurses

main.o: main.c errors.c util.c
USERNAME := $(shell whoami >> username.txt)
GIT:= $(shell head -n -1 openshell.h > temp.txt ; mv temp.txt openshell.h;git describe --abbrev=4 --dirty --always --tags > VERSION; echo "\#define VERSION \"$(GIT_VERSION)\"" >> openshell.h)

.PHONY: clean
clean:
    rm -f *.o

`
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • 1
    you can make a additional phony target in makefile that writes `GIT_VERSION` to a source file. – user3528438 Apr 23 '16 at 17:05
  • Why have it in the header file at all? – Oliver Charlesworth Apr 23 '16 at 17:12
  • @OliverCharlesworth If I don't the `IDE` (Clion) complains and marks the variable red and won't build with cmake. – Niklas Rosencrantz Apr 23 '16 at 17:13
  • 1
    You surely need to have the makefile have a phony target in the makefile that writes GIT_VERSION to a .h file, only if the version has changed? Why? Because what if your GIT_VERSION is compiled in in main.c but you only change errors.c... your GIT_VERSION would not be updated unless there's a file dependency to force it to change. But you don't want to recompile every time if the version does not change, so your phony target has to compare against the existing version and only update the .h if it has changed. See the [git's own Makefile](https://github.com/git/git/blob/master/Makefile) – Mort Apr 23 '16 at 18:10
  • @Mort I want to be able to use a variable in my code that is the git version. – Niklas Rosencrantz Apr 27 '16 at 00:57
  • @Mort Now it works with Ubuntu (but maybe dangerous deleting the old version number in the .h file) `GIT:= $(shell head -n -1 openshell.h > temp.txt ; mv temp.txt openshell.h;git describe --abbrev=4 --dirty --always --tags > VERSION; echo "\#define VERSION \"$(GIT_VERSION)\"" >> openshell.h) ` – Niklas Rosencrantz Apr 27 '16 at 05:48

1 Answers1

2

To avoid warning: "VERSION" redefined, wrap the #define with #ifndef:

#ifndef VERSION
  #define VERSION "v0.1a"
#endif
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307