Linux's simplest kernel module Hello World example, linuxhello

Source: Internet
Author: User

Linux's simplest kernel module Hello World example, linuxhello

Note:If you want to follow this practice, you need an arm Development Board and a kernel of the corresponding version (If You Want To compile and run it on a Linux host, refer to the end of this ArticleAppendix 1)

1. Create the following file in the drivers directory of the kernel of the corresponding version:

Module file tree

The file code is as follows:
/*Hello. c*/

#include <linux/init.h>#include <linux/module.h>static int hello_init(void){    printk(KERN_INFO "[init] Can you feel me?\n");    return 0;}static void hello_exit(void){    printk(KERN_INFO "[exit] Yes.\n");}module_init(hello_init);module_exit(hello_exit);MODULE_AUTHOR("Alan Wang <alan@wrcode.com>");MODULE_LICENSE("GPL");MODULE_DESCRIPTION("A simple Hello World Module");MODULE_ALIAS("A simple module");

/*Kconfig*/

# drivers/alan_test/Kconfigmenu "ALAN_TEST Driver "comment "ALAN_TEST Driver comment"config ALAN_TEST    bool "ALAN_TEST support"config HELLO    tristate "hello module test"    depends on ALAN_TESTendmenu

/*Makefile*/

# drivers/alan_test/Makefile# makefile for the ALAN_TESTobj-$(CONFIG_HELLO)    += hello.o
2. Modify the Kconfig Makefie in the upper-level directory:

Under driversKconfigAdd a row before endmenu

source "drivers/alan_test/Kconfig"endmenu

Under driversMakefileAdd a line at the end

obj-$(CONFIG_ALAN_TEST)        += alan_test/
3. make menuconfig Add the newly written module to the kernel:

In sequence: Device Drivers -->
ALAN_TEST Driver -->
* ** ALAN_TEST Driver comment ***
[*] ALAN_TEST support
<M> hello module test

ExitSaveMenuconfig

4. Compile the kernel:

UseMakeCompile
GeneratedZImage(Arch/arm/boot/zImage) is written to the corresponding position of the Development Board.
GeneratedHello. koCopy (drivers/alan_test/hello. ko) to a directory in the Linux System of the Development Board.

5. Insert the kernel module:

Insmod

6. Use modinfo to view the module information:

Modinfo

Appendix 1: The compilation module runs on the PC

Put hello. c and Makefile in the same directory
/*Makefile*/

KERNEL_VER = $(shell uname -r)# kernel modulesobj-m += hello.o# specify flags for the module compilationEXTRA_CFLAGS = -g -O0build: kernel_moduleskernel_modules:    make -C /lib/modules/$(KERNEL_VER)/build M=$(CURDIR) modulesclean:    make -C /lib/modules/$(KERNEL_VER)/build M=$(CURDIR) clean

Compile and runThe steps are as follows:
Compile and run the hello module on the PC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.