linux驱动学习1---Hello World模块

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/finewind/article/details/47165513

目的: 主要用于熟悉驱动程序编写及调试基本流程。

1.准备

         操作系统: ubuntu12.04 LTS

2.源码

#include <linux/init.h>
#include <linux/module.h>

static int hello_init(void)
{
	pr_info("Hello, World.\n");
	return 0;
}

static void hello_exit(void)
{
	pr_info("Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("Dual BSD/GPL");

3.Makefile

obj-m := hello.o

KERNELDIR := /usr/src/linux-headers-$(shell uname -r)
PWD := $(shell pwd)

modules:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

4.编译

make:
ckt@ubuntu:~/work/ldd/hello$ make
make -C /usr/src/linux-headers-3.13.0-32-genericM=/home/ckt/work/ldd/hello modules
make[1]: Entering directory`/usr/src/linux-headers-3.13.0-32-generic'
  CC [M]  /home/ckt/work/ldd/hello/hello.o
  Buildingmodules, stage 2.
  MODPOST 1modules
  LD [M]  /home/ckt/work/ldd/hello/hello.ko
make[1]: Leaving directory`/usr/src/linux-headers-3.13.0-32-generic'

5.测试

         lsmod                  #查看当前加载的模块
         sudo insmod hello.ko         #加载hello.ko模块
         sudo rmmod hello             #移除hello.ko模块
         cat/var/log/syslog     #查看日志


         

猜你喜欢

转载自blog.csdn.net/finewind/article/details/47165513