内核态程序Makefile

两个C文件, test1.c,test2.c,程序要引用上一级目录下"include"中的头文件,整个模块编译成test,Makefile编写如下:

##########################################################

ifneq ($(KERNELRELEASE),)

    obj-m := test.o

    test-objs := test1.o test2.o

    EXTRA_CFLAGS += -g -Wall

    EXTRA_CFLAGS += -I$(WORK_DIR)/../include

 

else

    KDIR := /lib/modules/$(shell uname -r)/build

    PWD := $(shell pwd)

    ifeq ($(WORK_DIR),)

        export WORK_DIR = $(PWD)

    endif

 

make:

    $(MAKE) -C $(KDIR) M=$(PWD) modules

clean:

    $(MAKE) -C $(KDIR) M=$(PWD) clean

 

endif

#############################################################

说明:

ifneq: if not equal

ifeq: if equal

当从命令行调用时,$(KERNELRELEASE)未被设置,第一行ifneq失败,走else流程,设置KDIR,PWD变量

在找到内核原码树后,走到了“make:”它会第二次调用make,此时KERNELRELEASE已设置,设置obj-m,

从而开始构造模块

你可能感兴趣的:(c,shell,include,makefile)