Set up the environment for driver compiling in Debian

时间:2021-03-01 17:48:39

  1、check the kernel version

$ uname -r
3.2.--amd64

  2、install the source code

$ sudo apt-get install linux-source-3.2

  then there would be a file in /usr/src

$ ls /usr/src | grep linux-source
linux-source-3.2.tar.bz2

  3、decompress the .bz2

$ sudo tar xvf linux-source-3.2.tat.bz2

  4、configure the files.

$ sudo make oldconfig
// or try the following method
$ sudo make menuconfig
$ sudo make xconfig

  5、compile the kernel

$ sudo make bzImage

  6、compile the modules

$ sudo make modules

  7、install the modules

$ sudo make modules_install

  8、reboot

$ sudo shutdown -r now

  9、try a helloworld module

//hello.c
#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 ;
} static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye cruel world\n");
} module_init(hello_init);
module_exit(hello_exit);

  Makefile

//Makefile
obj-m := hello.o
KERNELDIR := /lib/modules/3.2.46/build
PWD := $(shell pwd) modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

  then enter make

$ sudo make

  if the file hello.ko created, continue.

  10、test the module.

$ sudo insmod hello.ko
$ sudo rmmod hello.ko
$ cat /var/log/syslog | tail -
Jun :: Debian NetworkManager[]: <info> Activation (eth0) Stage of (IPv6 Configure Timeout) started...
Jun :: Debian NetworkManager[]: <info> Activation (eth0) Stage of (IPv6 Configure Timeout) complete.
Jun :: Debian ntpdate[]: adjust time server 59.66.173.165 offset 0.222044 sec
Jun :: Debian kernel: [ 347.185953] Hello,world
Jun :: Debian kernel: [ 351.580155] Goodbye cruel world
 

  more tips look at

http://hi.baidu.com/sgnkdomnrrejowr/item/5154dec4b94e4ddbef183b7c