设为首页 加入收藏

TOP

在Ubuntu下开发驱动程序
2014-11-24 13:04:55 来源: 作者: 【 】 浏览:1
Tags:Ubuntu 开发 驱动程序

首先,建立一个目录作为自己的工作目录,比如我的是~/kernel/mymodule。然后,新建一个hello.c文件,代码如下:
#include
#include
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卸载模块。
很多书上说要先下载源码才能编译模块,但我的测试是不用。当然,下载源码编译学习对驱动开发来说也是很重要的。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android平台下的AES加密和Java平.. 下一篇如何在Android源码里查找Java中na..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·python数据分析岗的 (2025-12-25 10:02:21)
·python做数据分析需 (2025-12-25 10:02:19)
·成为一个优秀的pytho (2025-12-25 10:02:16)
·Java后端面试实习自 (2025-12-25 09:24:21)
·Java LTS版本有哪些 (2025-12-25 09:24:18)