您好,登錄后才能下訂單哦!
這篇文章給大家介紹Linux下hello.ko內(nèi)核模塊制作怎么樣,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
在內(nèi)核目錄以外編譯ko文件
#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);
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
make
在該目錄下會(huì)生成hello.ko文件
在內(nèi)存目錄drivers/char編譯
#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);
menu "Character devices" config HELLO tristate "hello driver" depends on ARCH_S3C2440 help this is my first driver.
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
Device Drivers ---> Character devices ---> <M> hello driver
make SUBDIR=drivers/char/ modules
在內(nèi)核目錄下面的drivers/char/會(huì)生成hello.ko文件
在內(nèi)存目錄drivers/char下另建一個(gè)目錄
mkdir hellos
#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);
vi Makefile
# Makefile for the hellos driver # obj-$(CONFIG_HELLOS) += hellos.o
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.
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目錄
Device Drivers ---> Character devices ---> <M> hellos driver
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ò),可以把它分享出去讓更多的人看到。
免責(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)容。