Linux Driver Hello World

Source: Internet
Author: User

Look at the Windows internals for a few days, feel very annoying, see not go in. The author of the various mechanisms of Windows is a bit boring, after all, Windows is not open source, many internal implementations can only be explained by concepts, some abstractions.

Turning to the Linux camp, the classic ldd (Linux Driver Develop) reads seamlessly and brief encounter.

The driver of Hello World, also for several days, today only to fix. During the compilation and installation of their own kernel almost put the entire system to hang, the format is not cause make for several days inexplicable error.

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

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

Then you can find a place to write our own Hello World code, the code is relatively simple, with the VI is good.

#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 for module loading and unloading, just print it.

Only the code cannot be compiled directly with GCC, write the makefile file. Makefile file recommended to write with VI, save error.

Obj-m = Hello.okversion = $ (Shell uname-r) all:make-c/lib/modules/$ (kversion)/build m=$ (PWD) modulesclean:make-c/lib/m odules/$ (kversion)/build m=$ (PWD) Clean

Then make to 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 '
You can then load and unload the module test effect:

Insmod Hello.kormmod Hello.ko

The SSH command line may not see the effect, see DMESG:

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: 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.