Starting with this chapter, all the basic concepts of module and kernel programming are introduced and a complete module is written to practice these basic overviews
Read.
3.1 Building a test environment
Since all test codes are based on the Ubuntu 14.04.2 Desktop 3.16.0-30-generic Core, it is best to
Linux official website to download a copy of the source code for this version of the kernel. In addition, it is recommended to install your Ubuntu desktop environment inside the virtual machine, which
Hardware damage or critical data loss due to misoperation. For more test environment details, refer to the blog post:
Ubuntu 14.04.2 + VMware builds Linux-driven development environment
3.2 Hello World Module
The following code is a complete Hello World module.
#include <linux/init.h> #include <linux/module.h>module_license ("Dual BSD/GPL"), Static int Hello_init ( void) {PRINTK (Kern_alert "Hello, world\n"); return 0;} static void Hello_exit (void) {PRINTK (Kern_alert "Goodbye, cruel world\n");} Module_init (Hello_init); Module_exit (Hello_exit);
The module has two functions, Hello_init () is called when the module is loaded into the kernel, and Hello_exit () is called when the module is removed from the kernel.
The Macros Module_init and module_exit respectively put functions Hello_init () and Hello_exit () into two specific segments (sections), so that when the modules are linked to the kernel, the kernel can know that they are functions that are used for module loading/removal, respectively. and the Macro Module_license statement
The permissions of the module, and if you do not make a license statement, the kernel will complain.
This article is from the "Jimokuangxiangqu" blog, make sure to keep this source http://4594296.blog.51cto.com/4584296/1791050
Linux Device Driver Development Learning (3): Building and running Modules (unfinished)