在Ubuntu下开发驱动程序

首先,建立一个目录作为自己的工作目录,比如我的是~/kernel/mymodule。然后,新建一个hello.c文件,代码如下:

#include <linux/init.h>

#include <linux/module.h>

MODULE_LICENSE("Dual BSP/GPL");

static int hello_init(void)

{

   printk(KERN_ALERT "Hello,world.\n");  return 0;

}

static int hello_exit(void)

{

   printk(KERN_ALERT "Goodbye, cruel world.\n");  

  return 0;

}

module_init(hello_init);

module_exit(hello_exit);

再建立一个Makefile文件,注意文件名字是“Makefile”,不能是“makefile”或其它名字。文件内容如下:

# If KERNELREALEASE is defined, we've been invoked from the

# kernel build system and can use its language

ifneq ($(KERNELRELEASE),)

   obj-m := hello.o

# Otherwise we were called directly from the command

# line; invoke the kernel build system

else

   KERNELDIR ?= /usr/src/linux-headers-3.2.0-23-generic-pae/  

  PWD := $(shell pwd)

default:

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

endif

其中,“linux-headers-3.2.0-23-generic-pae”是我机器上使用的内核版本。可以使用“uname -a”命令查看。 然后,直接在命令行敲入make就可以编译了。

编译成功后,使用sudo insmod ./hello.ko安装,lsmod查看已经安装的模块,rmmod hello卸载模块。 很多书上说要先下载源码才能编译模块,但我的测试是不用。当然,下载源码编译学习对驱动开发来说也是很重要的。

转载于:https://www.cnblogs.com/zxglive2006/archive/2012/04/28/2475814.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值