unix domain socket相关
D-Bus

内核模块编译

lazycat posted @ 2012年3月02日 21:27 in linux , 1748 阅读

Makefile文件

# 内核文件名称
obj-m := hello.o
# 内核源码地址     
KDIR := /lib/modules/$(shell uname -r)/build
# 当前路径
PWD = $(shell pwd)

default:
     make -C $(KDIR) SUBDIRS=$(PWD)  modules
clean:
     rm rf Module.symvers *.o *.mod.c *.cmd .tmp_versions

源文件hello.c

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

static int hello_init(void)
{
        printk("helloworld init\n");
        return 0;
}

static void hello_exit(void)
{
        printk("helloworld exit\n");
        return;
}

module_init(hello_init);
module_exit(hello_exit);    

然后执行make 生成内核模块hello.ko

加载模块 insmod hello.ko

卸载模块 rmmod hello.ko

查看模块 lsmod 

Module                  Size  Used by
hello                  12496  0 

Used by表示这个模块被引用的次数,只有为0的时候才可以卸载模块

内核使用printk打印信息,可以用命令dmesg查看log

[  412.605328] helloworld init
[  433.266742] helloworld exit

找到一个很详细的内核模块加载的文章

/user_files/lazycat/File/《深入Linux设备驱动程序内核机制》第1章 内核模块.pdf

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter