在Makefile中检查gcc版本? [英] Checking the gcc version in a Makefile?
问题描述
我想使用一些gcc警告开关,这些警告开关在旧的gcc版本中不可用(例如。-Wtype-limits)。
I would like to use some gcc warning switchs that aren't available in older gcc versions (eg. -Wtype-limits).
有没有简单的方法检查gcc版本,只添加这些额外的选项,如果使用最近的gcc?
Is there an easy way to check the gcc version and only add those extra options if a recent gcc is used ?
推荐答案
我不会说它容易,但是你可以使用GNU make的 shell
函数执行shell命令,比如 gcc --version
,然后使用 ifeq
条件表达式来检查版本号并适当地设置你的 CFLAGS
变量。
I wouldn't say its easy, but you can use the shell
function of GNU make to execute a shell command like gcc --version
and then use the ifeq
conditional expression to check the version number and set your CFLAGS
variable appropriately.
以下是makefile的一个简单示例:
Here's a quick example makefile:
CC = gcc
GCCVERSION = $(shell gcc --version | grep ^gcc | sed 's/^.* //g')
CFLAGS = -g
ifeq "$(GCCVERSION)" "4.4.3"
CFLAGS += -Wtype-limits
endif
all:
$(CC) $(CFLAGS) prog.c -o prog
编辑:没有 ifgt
。但是,您可以使用shell expr
命令进行大于比较。这里是一个例子
There is no ifgt
. However, you can use the shell expr
command to do a greater than comparison. Here's an example
CC = gcc
GCCVERSIONGTEQ4 := $(shell expr `gcc -dumpversion | cut -f1 -d.` \>= 4)
CFLAGS = -g
ifeq "$(GCCVERSIONGTEQ4)" "1"
CFLAGS += -Wtype-limits
endif
all:
$(CC) $(CFLAGS) prog.c -o prog
这篇关于在Makefile中检查gcc版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!