LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1514|回复: 0

刚看ldd3,写的hello程序

[复制链接]
发表于 2010-12-3 18:21:50 | 显示全部楼层 |阅读模式
设备驱动程序试水,后续也会到版上请教些问题,希望大虾们多多指教啊。

1. 准备内核环境
在内核源代码文件夹(/user/src/linux-2.6*)下执行:
make oldconfig #配置内核支持动态加载模块
make prepare
make scripts

2. 编写hello模块(hello.c)
#include "linux/init.h"
#include "linux/module.h"

static int __init hello_init(void)
{
    printk(KERN_ALERT "hello\n");
    return 0;
}

static void __exit hello_exit(void)
{
    printk(KERN_ALERT "bye.\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("LGPL");
MODULE_DESCRIPTION("a hello module for test");

3. 编写Makefile
obj-m := hello.o
KERN_DIR='/usr/src/linux-2.6.16.60-0.54.5'
all:
        $(MAKE) -C $(KERN_DIR) M=`pwd`
clean:
        rm -f *.o *.ko
4. 编译和装卸
make
insmod hello.ko
lsmod | grep hello
rmmod hello.ko
注:如果终端不打印结果,可以通过 tail /var/log/messages查看结果
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表