Linux simplest kernel module Hello World Example

Source: Internet
Author: User

Note: If you want to follow this practice, you need to have a functioning arm development Board and a corresponding version of the kernel (if you want to compile and run on a Linux host, please refer to the end of the 1)

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

Where 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 <[email protected]>"); 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 s Upport "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 of the previous level of the directory:

Drivers Kconfig End Endmenu add a row before

SOURCE "Drivers/alan_test/kconfig" Endmenu

Add a line to the end of Makefile under drivers

obj-$ (config_alan_test) + = alan_test/
3. Make Menuconfig Add the newly-written module to the kernel:

In turn: Device drivers-->
Alan_test driver-->
Alan_test Driver Comment * * *
[*] Alan_test Support
< M > Hello module test

Exit Save Menuconfig

4. Compile the kernel:

Compiling with make
The generated zimage (arch/arm/boot/zimage) is burned to the corresponding location of the Development Board.
The generated Hello.ko(Drivers/alan_test/hello.ko) is copied to a directory on the Development Board Linux system.

5. Insert the kernel module:

6. Use Modinfo to view module information:

Modinfo

Attachment 1: Compile the module to run 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 run steps such as:


Linux simplest kernel module Hello World Example

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.