Linux下hello.ko内核模块制作

本文详细介绍了在Linux环境下编译内核模块的三种常见方法:在内核目录外、在内核源码drivers/char目录下及在该目录新建子目录中编译ko文件的过程。每种方法均包括编写模块代码、Makefile配置、内核配置及编译步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、方法一

在内核目录以外编译ko文件

1、编写hello模块代码

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("kent");

static int __init hello_init()
{
        printk(KERN_ALERT "hello, world - this is the kernel speaking!\n");
        return 0;
}

static void __exit hello_exit()
{
        printk(KERN_ALERT "short is the life of a kernel module!\n");
}

module_init(hello_init);
module_exit(hello_exit);

2、编写hello模块的Makefile文件

ifneq ($(KERNELRELEASE),)

obj-m:=hello.o

else
    
KERNELDIR?=/opt/arm/linux-2.6.30.4/
PWD := $(shell pwd)

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

modules_install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

.PHONY: modules modules_install clean

endif

3、编译

make

在该目录下会生成hello.ko文件

二、方法二

在内存目录drivers/char编译

1、编写hello模块代码

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("kent");
 
static int __init hello_init()
{
        printk(KERN_ALERT "hello, world - this is the kernel speaking!\n");
        return 0;
}
 
static void __exit hello_exit()
{
        printk(KERN_ALERT "short is the life of a kernel module!\n");
}
 
module_init(hello_init);
module_exit(hello_exit);

2、在内核源码中添加对hello驱动的支持

menu "Character devices"

config HELLO
        tristate "hello driver"
        depends on ARCH_S3C2440
        help
          this is my first driver.

3、修改同目录下的Makefile文件

FONTMAPFILE = cp437.uni

obj-y    += mem.o random.o tty_io.o n_tty.o tty_ioctl.o tty_ldisc.o tty_buffer.o tty_port.o

obj-$(CONFIG_HELLO)             += hello.o
obj-$(CONFIG_LEGACY_PTYS)       += pty.o
obj-$(CONFIG_UNIX98_PTYS)       += pty.o

4、配置内核

Device Drivers  ---> 
    Character devices  --->
        <M> hello driver

5、编译

make SUBDIR=drivers/char/ modules

在内核目录下面的drivers/char/会生成hello.ko文件

三、方法三

在内存目录drivers/char下另建一个目录

1、创建目录

mkdir hellos

2、编写hello模块代码

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("kent");
 
static int __init hello_init()
{
        printk(KERN_ALERT "hello, world - this is the kernel speaking!\n");
        return 0;
}
 
static void __exit hello_exit()
{
        printk(KERN_ALERT "short is the life of a kernel module!\n");
}
 
module_init(hello_init);
module_exit(hello_exit);

3、在hellos目录创建Makefile文件

vi Makefile
# Makefile for the hellos driver
#

obj-$(CONFIG_HELLOS) += hellos.o

4、修改drivers/char目录的Kconfig

menu "Character devices"

config HELLO
        tristate "hello driver"
        depends on ARCH_S3C2440
        help
          this is my first driver.

config HELLOS
        tristate "hellos driver"
        depends on ARCH_S3C2440
        help
          this is my second driver.

5、修改drivers/char目录的Makefile

obj-y    += mem.o random.o tty_io.o n_tty.o tty_ioctl.o tty_ldisc.o tty_buffer.o tty_port.o

obj-$(CONFIG_HELLO)             += hello.o
obj-$(CONFIG_HELLOS)            += hellos/ #这个是刚刚创建的hellos目录

6、配置内核

Device Drivers  ---> 
    Character devices  --->
        <M> hellos driver

7、编译

make SUBDIR=drivers/char/ modules
在内核目录下面的drivers/char/hellos/会生成hellos.ko文件


注意:内核一定要先make,要不然会报错。



转载于:https://my.oschina.net/u/1759641/blog/607561

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值