Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使我的程序使用BSD打印其github版本号?_C_Git_Gcc_Makefile_Openbsd - Fatal编程技术网

C 如何使我的程序使用BSD打印其github版本号?

C 如何使我的程序使用BSD打印其github版本号?,c,git,gcc,makefile,openbsd,C,Git,Gcc,Makefile,Openbsd,我希望能够在代码中使用git版本的变量。如果我用make设置变量,然后在.h文件中重写它,那么打印的最终值将从.h”文件中删除。是否有方法覆盖makefile中的.h 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: "V

我希望能够在代码中使用git版本的变量。如果我用
make
设置变量,然后在
.h
文件中重写它,那么打印的最终值将从.h”文件中删除。是否有方法覆盖makefile中的
.h

 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"
我正试图从github设置变量
VERSION
,以便查看版本:

$ ./a.out --version
OpenShell version 0.1(a)
Version: v0.1a-2-gc6b1-dirty
我有这个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
然后它在Linux Ubuntu上工作,但在BSD上不工作,对于PC-BSD,变量未显示:

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 $ 
更新160426 07:49 CET 现在它可以与Ubuntu一起使用(但删除.h文件中的旧版本号可能会很危险)`GIT:=$(shell head-n-1 openshell.h>temp.txt;mv temp.txt openshell.h;GIT description--abbrev=4--dirty--always--tags>version;echo“#define version\”$(GIT\u version)\”>>openshell.h)

更新的Makefile现在是:

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

`
为避免警告:“版本”被重新定义,请将#定义用#ifndef括起来:


你可以在makefile中创建一个额外的虚假目标,将
GIT_VERSION
写入源文件。为什么要将它放在头文件中呢?@OliverCharlesworth如果我没有
IDE
(Clion)抱怨并将变量标记为红色,并且不会使用cmake生成。您确实需要让makefile在makefile中有一个虚假目标,该目标将GIT_版本写入.h文件,只有在版本发生更改时才可以?为什么?因为如果您的GIT_版本是在main.c中编译的,但您只更改了errors.c。。。您的GIT_版本将不会更新,除非存在强制更改的文件依赖关系。但是,如果版本没有更改,您不希望每次都重新编译,因此您的虚假目标必须与现有版本进行比较,并且仅在.h发生更改时更新它。请参阅@Mort,我希望能够在代码中使用git版本的变量。
#ifndef VERSION
  #define VERSION "v0.1a"
#endif