溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Linux下hello.ko內(nèi)核模塊制作怎么樣

發(fā)布時(shí)間:2021-10-22 10:00:39 來(lái)源:億速云 閱讀:285 作者:柒染 欄目:互聯(lián)網(wǎng)科技

這篇文章給大家介紹Linux下hello.ko內(nèi)核模塊制作怎么樣,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

一、方法一

在內(nèi)核目錄以外編譯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

在該目錄下會(huì)生成hello.ko文件

二、方法二

在內(nèi)存目錄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、在內(nèi)核源碼中添加對(duì)hello驅(qū)動(dòng)的支持

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、配置內(nèi)核

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

5、編譯

make SUBDIR=drivers/char/ modules

在內(nèi)核目錄下面的drivers/char/會(huì)生成hello.ko文件

三、方法三

在內(nèi)存目錄drivers/char下另建一個(gè)目錄

1、創(chuàng)建目錄

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目錄創(chuàng)建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/ #這個(gè)是剛剛創(chuàng)建的hellos目錄

6、配置內(nèi)核

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

7、編譯

make SUBDIR=drivers/char/ modules

在內(nèi)核目錄下面的drivers/char/hellos/會(huì)生成hellos.ko文件


 

注意:內(nèi)核一定要先make,要不然會(huì)報(bào)錯(cuò)。


關(guān)于Linux下hello.ko內(nèi)核模塊制作怎么樣就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI