Although there are already many introductions on the Linux driver development environment on the Internet, as a newbie who has just been involved in the Linux driver, all the things have to be done by yourself, it usually takes a lot of time if the configuration is incorrect or cannot be used. Let's review the building process.
Kernel version: 2.6.38
Operating System: Ubuntu 11.04
Development Environment setup process:
1. Download the necessary development kit (binutils)
Sudo apt-Get install build-essential
2. Download the Linux source code (the specific version must be consistent with the current OS kernel version, uname-a query)
Sudo apt-Get install linux-source-2 .. 6.38 <remove: sudo apt-Get remove linux-source-2.6.38>
After downloading, go to the/usr/src directory and find the Linux source code package. Run tar-xvf to unpackage the package.
3. Go to the decompressed directory and perform the following operations. <if you do not want to input sudo every time, you can run Sudo-s once. As long as the current terminal does not exit, the root permission will be retained.>
Configure sudo make oldconfig
It takes some time for sudo make to compile the kernel and wait patiently.
Sudo make modules compilation Module
Sudo make modules_install installation Module
After the above operations are performed, the Ubuntu driver development environment is basically set up. Next we will use the classic Hello world for testing.
Hello. C:
#include <linux/init.h>#include <linux/module.h>static int hello_init(void){ printk(KERN_ALERT "Hello World \n"); return 0;}static void hello_exit(void){ printk(KERN_ALERT "Goodbye \n");}module_init(hello_init);module_exit(hello_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("jiangming.fu@163.com");
Makefile is as follows:
obj-m := hello.oKDIR= /lib/modules/$(shell uname -r)/buildPWD=$(shell pwd)all:$(MAKE) -C $(KDIR) M=$(PWD) #modules.PHONY:cleanclean:rm -f *.mod.c *.mod.o *.ko *.o *.tmp_versions
After make is executed, hell is generated in the current directory. ko (Driver Module). You can use sudo insmod (rmmod) Hello. ko installation (uninstall) module. you can view the output information of the driver module through dmsg.