Linux DRIVER: Hello World, linuxhelloworld

Source: Internet
Author: User

Linux DRIVER: Hello World, linuxhelloworld

After reading Windows Internals for a few days, I felt quite annoyed and couldn't see it. The author talks about the various mechanisms of Windows. After all, Windows is not open-source. Many internal implementations can only be explained by concepts and abstract.

Turning to the Linux camp, the classic LDD (Linux Driver Develop) can be read smoothly and never stops waiting.

The driver Hello World is also good for several days. In the meantime, compiling and installing your own kernel almost screwed up the entire system. The format was incorrect, which caused the "make" failure for several days.

First of all, I am from Ubuntu and apt is very convenient. You do not need to use your own compiled kernel. Ubuntu can directly use apt to install the relevant compiling environment (kernel header files and library files ). The name is linux-headers-kernel version. If you do not know the kernel version, use the following command:

$ sudo apt-get install linux-headers-$(uname -r)


Then we can find a place to write our own Hello World code. The code is relatively simple, just use vi.

#include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h>static int hello_init(void){printk( KERN_ALERT "Hello, world! I'm Lizhixing.\n" );return 0;};static void hello_exit(void){printk( KERN_ALERT "Good bye, cruel world. Tommorow is another day! \n" );};module_init( hello_init );module_exit( hello_exit );

This code registers the event handler function loaded and uninstalled by the module and only prints it.

Only the Code cannot be directly compiled using gcc. You need to write the Makefile file. We recommend that you write Makefile files in vi to save errors.

obj-m = hello.oKVERSION = $(shell uname -r)all:make -C /lib/modules/$(KVERSION)/build M=$(PWD) modulesclean:make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

Then make can complete the compilation.

makemake -C /lib/modules/3.13.0-24-generic/build M=/disks/e/0-code/0-thinkingl-code/trunk/thinkingl-codelib/Study/0-linuxdriver/0-helloworld modulesmake[1]: Entering directory `/usr/src/linux-headers-3.13.0-24-generic'  CC [M]  /disks/e/0-code/0-thinkingl-code/trunk/thinkingl-codelib/Study/0-linuxdriver/0-helloworld/hello.o  Building modules, stage 2.  MODPOST 1 modules  CC      /disks/e/0-code/0-thinkingl-code/trunk/thinkingl-codelib/Study/0-linuxdriver/0-helloworld/hello.mod.o  LD [M]  /disks/e/0-code/0-thinkingl-code/trunk/thinkingl-codelib/Study/0-linuxdriver/0-helloworld/hello.komake[1]: Leaving directory `/usr/src/linux-headers-3.13.0-24-generic'
Then you can load and detach the module to test the effect:

insmod hello.kormmod hello.ko


The ssh command line may not be able to see the effect. See dmesg:

[133540.841098] Hello, world! I'm Lizhixing.[133553.819340] Good bye, cruel world. Tommorow is another day! 






Linux-driven hello world cannot be compiled

Write a Makefile. Note that tab

Obj-m: = drv. o
KDIR: =/lib/modules/$ (shell uname-r)/build
PWD: = $ (shell pwd)

Default:
$ (MAKE)-C $ (KDIR) SUBDIRS = $ (PWD) modules
Clean:
$ (MAKE)-C $ (KDIR) SUBDIRS = $ (PWD) clean
Rm-rf Module. markers modules. order Module. symvers

Set up the hello world module for linux Device Drivers

You do not need to make in the source code directory. refer to the following. It has been used before and is easy to use.
Module source code file led_driver.c
# Ifndef _ KERNEL __# define _ KERNEL __# endif # ifndef MODULE # define MODULE # endif # include <linux/module. h> # include <linux/kernel. h> MODULE_LICENSE ("GPL"); int init_led_module (void) {printk ("Hello led driver! \ N "); return 0;} void cleanup_led_module (void) {printk (" Goodbye led driver! \ N ");} MODULE_AUTHOR (" ABC "); MODULE_LICENSE (" Dual BSD/GPL "); module_init (init_led_module); module_exit (cleanup_led_module );
Makefile
Ifneq ($ (KERNELRELEASE),) CROSS_COMPILE = ARCH: = x86CC :=$ (CROSS_COMPILE) gccLD :=$ (CROSS_COMPILE) ldobj-m: = led_driver.o elsePWD = $ (shell pwd) KVER? = $ (Shell uname-r) KDIR: =/lib/modules/$ (KVER)/builddefault: $ (MAKE)-C $ (KDIR) M = $ (PWD) modulesclean: $ (MAKE)-C $ (KDIR) M = $ (PWD) clean rm-rf. *. cmd *. o *. mod. c *. ko. tmp_versionsendif

Related Article

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.