Ubuntu 16.04 Linux driver compiling step 1, ubuntu16.04
Build the source code Environment
> Sudo apt-cache search linux-source
Linux-source-Linux kernel source with Ubuntu patches
Linux-source-4.4.0-Linux kernel source for version 4.4.0 with Ubuntu patches
Because the difference is slightly different, choose linux-source-4.4.0. Depends on the version number you display.
> Sudo apt-get install linux-source-4.4.0
After the installation is complete, two new files, one for the linux-source-4.4.0 folder and the other for the linux-source-4.4.0.tar.bz2, appear under the/usr/src/directory.
> Tar jxvf linux-source-4.4.0.tar.bz2-C/home/yourdir/Kernel
Decompress the package to your folder or to the current directory.
Perform Kernel configuration in the extracted linux-source-4.4.0 directory
> Sudo make oldconfig// Configure the kernel
> Sudo make// It takes a long time to compile the kernel. h No such file or directory indicates that libssl-dev needs to be installed to execute sudo apt-get install libssl-dev.
> Sudo make modules// Compile the module
> Sudo make modules_install// Installation Module
The first. c file and Makefile file
- Create a folder in your working directory and create the hello. c and Makefile files.
- Makefile can be directly created using vim Makefile. Note that M is capitalized.
Hello. c code
1 #include <linux/init.h> 2 #include <linux/module.h> 3 MODULE_LICENSE("Dual BSD/GPL"); 4 static int hello_init(void) 5 { 6 printk(KERN_ALERT "Hello, world\n"); 7 return 0; 8 } 9 static void hello_exit(void)10 {11 printk(KERN_ALERT "Goodbye, cruel world\n");12 }13 14 module_init(hello_init);15 module_exit(hello_exit);
Makefile. // Note the space and Tab
Obj-m: = hello. ohellomodule-objs: = module # You can change "hello" to your name: KERNELDIR: =/lib/modules/$ (shell uname-r)/buildPWD: = $ (shell pwd) default: $ (MAKE)-C $ (KERNELDIR) M = $ (PWD) modules
Save and run> Sudo make
To generate the hello. ko file. You can use this file to load it to the kernel.
Load command> sudo insmod./hello. ko
After loading, you can use> lsmod // to view the currently installed driver module.
> Cat/var/log/syslog, you can directly view the output hello and Goodbye // or use> dmesg | the tail command is simpler.
Uninstall command> sudo rmmod./hello. ko
The first time I configured the environment and wrote Makefile, I encountered many pitfalls. For more information about the driver, please refer to related books and tutorials on Linxu driver development.
For the reason why the hello. c and Makefile files in this article are written like this, I believe you can understand the relevant information.