Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to make hello.ko kernel module under Linux?

Shulou Source: shulou.com Published: 2022-06-02 01:44:42 09月30日 Update

This article introduces how to make the hello.ko kernel module under Linux. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

1. Method 1

Compile ko files outside the kernel directory

1. Write hello module code # include # include # include 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. Write the Makefile file ifneq of hello module ($(KERNELRELEASE)) ) obj-m:=hello.oelse KERNELDIR?=/opt/arm/linux-2.6.30.4/PWD: = $(shell pwd) modules: $(MAKE)-C $(KERNELDIR) mm $(PWD) modulesmodules_install: $(MAKE)-C $(KERNELDIR) M é n $(PWD) modules_installclean: rm-rf * .o * ~ core .depend. * .cmd * .ko * .mod.c .tmp _ versions.PHONY: modulesmodules_install cleanendif3, compile make

A hello.ko file will be generated in this directory

2. Method 2

Drivers/char compilation in memory directory

1. Write hello module code # include # include # include 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. Add hello driver support for menu "Character devices" config HELLO tristate "hello driver" depends on ARCH_S3C2440 help this is my first driver.3 to the kernel source code. Modify the Makefile file FONTMAPFILE = cp437.uniobj-y + = mem.o random.o tty_io.o n_tty.o tty_ioctl.o tty_ldisc.o tty_buffer.o tty_port.oobj-$ (CONFIG_HELLO) + = hello.oobj-$ (CONFIG_LEGACY_PTYS) + = pty.oobj-$ (CONFIG_UNIX98_PTYS) + = pty.o4 in the same directory, configure kernel Device Drivers-- > Character devices-> hello driver5, Compile make SUBDIR=drivers/char/ modules

The drivers/char/ under the kernel directory generates the hello.ko file

Third, method three

Create another directory under the memory directory drivers/char

1. Create directory mkdir hellos2, write hello module code # include # include # include 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. Create the Makefile file vi Makefile# Makefile for the hellos driver#obj-$ (CONFIG_HELLOS) + = hellos.o4 in the hellos directory, modify the Kconfigmenu "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 of the drivers/char directory, Modify the Makefileobj-y + = mem.o random.o tty_io.o n_tty.o tty_ioctl.o tty_ldisc.o tty_buffer.o tty_port.oobj-$ (CONFIG_HELLO) + = hello.oobj-$ (CONFIG_HELLOS) + = hellos/ # of the drivers/char directory. This is the hellos directory you just created. 6. Configure kernel Device Drivers-- > Character devices-> hellos driver7, compile make SUBDIR=drivers/char/ modules

The drivers/char/hellos/ under the kernel directory generates the hellos.ko file

Note: the kernel must make first, or an error will be reported.

On the Linux hello.ko kernel module production how to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Directories kernels files modules compilation code methods generation production content inventory more face-to-face help configuration good interest guys buddies articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux macOS OPPO Reno Shulou Information