内核模块的编译、加载与卸载

    xiaoxiao2021-03-25  167

    1.放在drivers/char/下

    拷贝hello.c到drivers/char/目录下

    打开drivers/char/Makefile,在其中增加一行:

    obj-m +=hello.o 或若有配置选项,增加如下一行:

    obj-$(CONFIG_HELLO) += hello.o

    打开配置文件drivers/char/Kconfig,在其中增加一项:

    config HELLO

    tristate “New Hello”

    cd usr/src/linux-2.6.34.13/

    make menuconfig

    make modules

    cd usr/src/linux-2.6.34.13/drivers/char/

    insmod ./hello.ko

    rmmod hello

    cat /var/log/messages | grep world

    2.

    放在drivers/char/下的子目录中

    在 drivers/char/下创建子目录hello

    拷贝hello.c到drivers/char/hello/目录下

    打开drivers/char/Makefile,在其中增加一行:

    obj-m +=hello/ 或若有配置选项,增加如下一行:

    obj-$(CONFIG_HELLO) += hello/

    在drivers/char/hello/下新建Makefile,需一行:

    obj-m += hello.o 或若有配置选项:

    obj-$(CONFIG_HELLO) += hello.o

    打开配置文件drivers/char/Kconfig,在其中增加一项:

    config HELLO

    tristate “New Hello”

    make menuconfig

    make modules

    cd usr/src/linux-2.6.34.13/drivers/char/hello/

    insmod ./hello.ko

    rmmod hello

    cat /var/log/messages | grep world

     

    3.

    放在内核源代码外

    建hello目录,将hello.c与makefile放入其下,makefile中只需一行:

    Obj-m :=hello.o

    在hello目录下运行命令:

    make -C ~/kernel-2.6 M=$PWD modules

    make -C /usr/src/linux-2.6.34.13/ M=$PWDmodules

    cd /home/ldd/hello/

    insmod ./hello.ko

    rmmod hello

    cat /var/log/messages | grep world

     

    4.

    建hello目录,将hello.c与makefile放入其下,使用带条件语句的复杂makefile(见后)

    # If KERNELRELEASE is defined, we've beeninvoked from the

    # kernel build system and can use itslanguage.

    ifneq ($(KERNELRELEASE),)

             obj-m:= hello.o

    # Otherwise we were called directly fromthe command

    # line; invoke the kernel build system.

    else

             KERNELDIR?= /usr/src/linux-2.6.34.13/ 

             PWD:= $(shell pwd)

    default:

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

    endif

    在hello目录下运行命令:

    make

    cd /home/ldd/hello/

    insmod ./hello.ko

    rmmod hello

    cat /var/log/messages | grep world

     

    转载请注明原文地址: https://ju.6miu.com/read-5052.html

    最新回复(0)