Skip to main content
 首页 » 操作系统

Linux 驱动模块(4)——模块编译

2022年07月19日44lhb25

1.Kernel中的Makefile将多个文件编译成一个模块

Example1: drivers/usb/core/Makefile:
usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o usbcore-y += devio.o notify.o generic.o quirks.o devices.o usbcore-y += port.o usbcore-$(CONFIG_OF) += of.o usbcore-$(CONFIG_USB_PCI) += hcd-pci.o usbcore-$(CONFIG_ACPI) += usb-acpi.o obj-$(CONFIG_USB) += usbcore.o obj-$(CONFIG_USB_LEDS_TRIGGER_USBPORT) += ledtrig-usbport.o
Example2:net/rfkill/Makefile 
rfkill-y            += core.o 
rfkill-$(CONFIG_RFKILL_INPUT)    += input.o 
obj-$(CONFIG_RFKILL)        += rfkill.o 
obj-$(CONFIG_RFKILL_GPIO)    += rfkill-gpio.o 
 
只有obj-y += xxxx.o 才会被编译成xxxx.ko文件

2.使用Yocto内核模块编译方法(多文件)

usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o 
usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o 
usbcore-y += devio.o notify.o generic.o quirks.o devices.o 
usbcore-y += port.o 
usbcore-y += of.o 
 
obj-m += usbcore.o 
 
KDIR := /media/ubuntu/work/Yocto3_4/build/tmp/work/g6m-poky-linux/linux-renesas/4.14.35-r1/build/ 
 
all: 
    make -C $(KDIR) M=$(PWD) modules CROSS_COMPILE=aarch64-poky-linux- ARCH=arm64 
    rm -f *.o *.order *.mod.c *.symvers *~ 
 
clean: 
    rm -f *.o *.order *.mod.c *.symvers *~ *.ko 
 
#补充 
clean: 
    make -C $(KERN_DIR) M=`pwd` modules clean #可实现自动删除 
    rm -rf modules.order

3.将单个驱动文件编译成模块

SRC := ehci-hcd 
obj-m := $(SRC).o 
KDIR := /media/ubuntu/work/Yocto3_4/build/tmp/work/g6m-poky-linux/linux-renesas/4.14.35-r1/build 
 
all: 
    #make -C $(KDIR) M=$(PWD) modules  ARCH=arm64(使用时这个删除并且下面给tab键) 
    make -C $(KDIR) M=$(PWD) modules CROSS_COMPILE=aarch64-poky-linux- ARCH=arm64 
 
clean: 
    rm -f *.o *.order *.mod.c *.symvers *~ *.ko

4.Ubuntu主机模块编译

obj-m += leds_4412.o 
 
all: 
    make -C /lib/modules/`uname -r`/build M=$(PWD) 
    rm -rf *.o *.o.cmd *.ko.cmd *.order *.symvers *.mod.c .tmp_versions 
 
clean: 
    rm -rf *.ko

5. Makefile中使用宏选择模块,和使用内核版本选择模块

ifeq ($(CONFIG_FEATURE_SELECT), y) 
 
ifeq ($(VERSION).$(PATCHLEVEL), 5.4) 
obj-y += feature_select_v1.o 
endif 
 
else 
        obj-y += feature_select_v2.o 
endif

参考:
Documentation/kbuild
Documentation/kbuild/modules.txt里面介绍了如何编译成模块。


本文参考链接:https://www.cnblogs.com/hellokitty2/p/9346711.html