【发布时间】:2013-02-25 09:00:31
【问题描述】:
如何在我的 Makefile 中检查我的内核版本??
根据内核版本,我想相应地选择一些头文件。
【问题讨论】:
如何在我的 Makefile 中检查我的内核版本??
根据内核版本,我想相应地选择一些头文件。
【问题讨论】:
KVER = $(shell uname -r)
KMAJ = $(shell echo $(KVER) | \
sed -e 's/^\([0-9][0-9]*\)\.[0-9][0-9]*\.[0-9][0-9]*.*/\1/')
KMIN = $(shell echo $(KVER) | \
sed -e 's/^[0-9][0-9]*\.\([0-9][0-9]*\)\.[0-9][0-9]*.*/\1/')
KREV = $(shell echo $(KVER) | \
sed -e 's/^[0-9][0-9]*\.[0-9][0-9]*\.\([0-9][0-9]*\).*/\1/')
kver_ge = $(shell \
echo test | awk '{if($(KMAJ) < $(1)) {print 0} else { \
if($(KMAJ) > $(1)) {print 1} else { \
if($(KMIN) < $(2)) {print 0} else { \
if($(KMIN) > $(2)) {print 1} else { \
if($(KREV) < $(3)) {print 0} else { print 1 } \
}}}}}' \
)
ifeq ($(call kver_ge,3,8,0),1)
echo great or equal than 3.8.0
else
echo less than 3.8.0
endif
【讨论】:
如果你正在编写一些应用程序,你可能会这样做
KERNELVERSION=$(shell uname -a)
或其他一些shell命令,可能是cat /proc/version
有关内核模块,请参阅cnicutar's answer。
【讨论】:
我没有 50 名声望,所以我无法在评论中回答 voght 先生的评论(投票给我,这样我就可以了!)但我可以这样做,就像另一个答案一样,所以就这样吧。
代码使用内置的 shell 来输出 (bash) 命令,如 uname 和 echo,并分配类似变量的宏例如KVER 到结果。 uname 提供内核版本,代码继续使用 unix sed(流编辑器;man sed for more)来提取每个结果中的主要、次要和版本号,并将它们分配给离散变量类宏。然后他分配一个宏名称,kver_ge 给测试是否内核版本大于作为参数提供的版本。
很酷,适合我。
【讨论】:
一种简单的方法是首先通过 Makefile 中的测试分配一个变量 true/false(实际上是下面示例中的 1/0),然后使用 ifeq 命令,就像 goodgoodstudydaydayup 的答案一样,这是一个更简单的方法:
KERNEL_GT_5_4 = $(shell expr `uname -r` \> "5.4.0-0-generic")
all:
ifeq (${KERNEL_GT_5_4},1)
echo greater than 5.4.0-0-generic
else
echo less than 5.4.0-0-generic
endif
【讨论】: