Set up the environment for driver compiling in Debian

  1、check the kernel version

$ uname -r
3.2.0-4-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
");
        return 0;
}

static void hello_exit(void)
{
        printk(KERN_ALERT "Goodbye cruel world
");
}

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 -5
Jun 22 03:05:49 Debian NetworkManager[2842]: <info> Activation (eth0) Stage 4 of 5 (IPv6 Configure Timeout) started...
Jun 22 03:05:49 Debian NetworkManager[2842]: <info> Activation (eth0) Stage 4 of 5 (IPv6 Configure Timeout) complete.
Jun 22 03:05:54 Debian ntpdate[3261]: adjust time server 59.66.173.165 offset 0.222044 sec
Jun 22 03:10:49 Debian kernel: [ 347.185953] Hello,world
Jun 22 03:10:54 Debian kernel: [ 351.580155] Goodbye cruel world
 

  more tips look at

http://hi.baidu.com/sgnkdomnrrejowr/item/5154dec4b94e4ddbef183b7c
原文地址:https://www.cnblogs.com/plinx/p/3149216.html