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

来源:互联网 发布:java 抽象工厂类 编辑:程序博客网 时间:2024/05/18 13:07

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

 

0 0