Linux编译模块问题
2014-01-29 19:23
330 查看
我的Linux版本是Redhat enterprise 2.6内核
以为主要参考
最简单的模块hello.c
/hello.c
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, worldn");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT"Goodbye, cruel worldn");
}
module_init(hello_init);
module_exit(hello_exit);
编译模块
内核代码在/usr/src/kernels/2.6.9-42.EL-smp-x86_64/下面
也存在于/lib/modules/2.6.9-42.ELsmp/build/
1,Makefile 文件
obj-m :=hello.o
再直接运行
make -C /usr/src/kernels/2.6.9-42.EL-smp-x86_64/ SUBDIRS=$PWD modules
得到结果
make: Entering directory `/usr/src/kernels/2.6.9-42.EL-smp-x86_64'
CC [M] /root/luckyball/device/hello.o
Building modules, stage 2.
MODPOST
CC /root/luckyball/device/hello.mod.o
LD [M] /root/luckyball/device/hello.ko
make: Leaving directory `/usr/src/kernels/2.6.9-42.EL-smp-x86_64'
生成结果
[root@localhost device]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile
2,Makefile 文件于上面一样
运行
make -C /lib/modules/2.6.9-42.ELsmp/build/ SUBDIRS=$PWD modules
得到运行结果
make: Entering directory `/usr/src/kernels/2.6.9-42.EL-smp-x86_64'
CC [M] /root/luckyball/device/hello.o
Building modules, stage 2.
MODPOST
CC /root/luckyball/device/hello.mod.o
LD [M] /root/luckyball/device/hello.ko
make: Leaving directory `/usr/src/kernels/2.6.9-42.EL-smp-x86_64'
可见两个内核代码是相关联的
生成结果 同上
3,直接写Makefile 文件
1 ifneq ($(KERNElRELEASE),)
2 obj-m := hello.o
3 else
4 obj-m := hello.o
5 KERNELDIR ?= /lib/modules/$(shell uname -r)/build
6 PWD := $(shell pwd)
7
8 default:
9 $(MAKE) -C $(KERNELDIR) SUBDIRS=$(PWD) modules
10 endif
其中/lib/modules/$(shell uname -r)/build 就是上面的
/lib/modules/2.6.9-42.ELsmp/build/
得到运行结果
make -C /lib/modules/2.6.9-42.ELsmp/build SUBDIRS=/root/luckyball/device modulesmake[1]: Entering directory `/usr/src/kernels/2.6.9-42.EL-smp-x86_64'
CC [M] /root/luckyball/device/hello.o
Building modules, stage 2.
MODPOST
CC /root/luckyball/device/hello.mod.o
LD [M] /root/luckyball/device/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.9-42.EL-smp-x86_64'
生成结果通上
注意: Makefile文件中的4行,是中没有的,我认为必需加,且我的实践证明也必需加
if ,else 两个独立的语句,都应该运行obj-m :=hello.o,即说明有一个模块需要从目标文件
hello.o构造,且名字为hello.ko.
装载和卸载模块
insmod ./hello.ko
rmmod hello
如果不能在终端打印消息,可以/var/log/syslog查看,也可以修改printk优先级使其在终端上先是,参看
31 #define KERN_EMERG "" /* system is unusable */
32 #define KERN_ALERT "" /* action must be taken immediately */
33 #define KERN_CRIT "" /* critical conditions */
34 #define KERN_ERR "" /* error conditions */
35 #define KERN_WARNING "" /* warning conditions */
36 #define KERN_NOTICE "" /* normal but significant condition */
37 #define KERN_INFO "" /* informational */
38 #define KERN_DEBUG "" /* debug-level messages */
把KERN_ALERT 改成 KERN_EMERG
问题自然就解决了!
以为主要参考
最简单的模块hello.c
/hello.c
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, worldn");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT"Goodbye, cruel worldn");
}
module_init(hello_init);
module_exit(hello_exit);
编译模块
内核代码在/usr/src/kernels/2.6.9-42.EL-smp-x86_64/下面
也存在于/lib/modules/2.6.9-42.ELsmp/build/
1,Makefile 文件
obj-m :=hello.o
再直接运行
make -C /usr/src/kernels/2.6.9-42.EL-smp-x86_64/ SUBDIRS=$PWD modules
得到结果
make: Entering directory `/usr/src/kernels/2.6.9-42.EL-smp-x86_64'
CC [M] /root/luckyball/device/hello.o
Building modules, stage 2.
MODPOST
CC /root/luckyball/device/hello.mod.o
LD [M] /root/luckyball/device/hello.ko
make: Leaving directory `/usr/src/kernels/2.6.9-42.EL-smp-x86_64'
生成结果
[root@localhost device]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile
2,Makefile 文件于上面一样
运行
make -C /lib/modules/2.6.9-42.ELsmp/build/ SUBDIRS=$PWD modules
得到运行结果
make: Entering directory `/usr/src/kernels/2.6.9-42.EL-smp-x86_64'
CC [M] /root/luckyball/device/hello.o
Building modules, stage 2.
MODPOST
CC /root/luckyball/device/hello.mod.o
LD [M] /root/luckyball/device/hello.ko
make: Leaving directory `/usr/src/kernels/2.6.9-42.EL-smp-x86_64'
可见两个内核代码是相关联的
生成结果 同上
3,直接写Makefile 文件
1 ifneq ($(KERNElRELEASE),)
2 obj-m := hello.o
3 else
4 obj-m := hello.o
5 KERNELDIR ?= /lib/modules/$(shell uname -r)/build
6 PWD := $(shell pwd)
7
8 default:
9 $(MAKE) -C $(KERNELDIR) SUBDIRS=$(PWD) modules
10 endif
其中/lib/modules/$(shell uname -r)/build 就是上面的
/lib/modules/2.6.9-42.ELsmp/build/
得到运行结果
make -C /lib/modules/2.6.9-42.ELsmp/build SUBDIRS=/root/luckyball/device modulesmake[1]: Entering directory `/usr/src/kernels/2.6.9-42.EL-smp-x86_64'
CC [M] /root/luckyball/device/hello.o
Building modules, stage 2.
MODPOST
CC /root/luckyball/device/hello.mod.o
LD [M] /root/luckyball/device/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.9-42.EL-smp-x86_64'
生成结果通上
注意: Makefile文件中的4行,是中没有的,我认为必需加,且我的实践证明也必需加
if ,else 两个独立的语句,都应该运行obj-m :=hello.o,即说明有一个模块需要从目标文件
hello.o构造,且名字为hello.ko.
装载和卸载模块
insmod ./hello.ko
rmmod hello
如果不能在终端打印消息,可以/var/log/syslog查看,也可以修改printk优先级使其在终端上先是,参看
31 #define KERN_EMERG "" /* system is unusable */
32 #define KERN_ALERT "" /* action must be taken immediately */
33 #define KERN_CRIT "" /* critical conditions */
34 #define KERN_ERR "" /* error conditions */
35 #define KERN_WARNING "" /* warning conditions */
36 #define KERN_NOTICE "" /* normal but significant condition */
37 #define KERN_INFO "" /* informational */
38 #define KERN_DEBUG "" /* debug-level messages */
把KERN_ALERT 改成 KERN_EMERG
问题自然就解决了!
相关文章推荐
- linux下源码编译 subversion(SVN)- 解决ra_dav模块加载问题(HTTP)
- linux设备驱动程序_hello word 模块编译各种问题集锦
- Linux下USB转串口的模块编译安装问题
- linux设备驱动程序_hello word 模块编译各种问题集锦
- Linux中编译apache服务器modules文件夹缺少模块(.so)的问题
- Linux中编译apache服务器modules文件夹缺少模块(.so)的问题
- linux 关于编译模块缺少config.h而产生错误的问题
- linux驱动程序多模块编译链接时的问题
- Linux内核编译,模块尺寸变大的解决办法
- linux 模块编译
- 在Linux下编译内核模块的Makefile的几种写法
- linux 系统 ko模块编译
- 解决Ubuntu 10.10下Vmware每次打开都会重新编译加载模块的问题
- 一个linux内核编译时遇到的perl语法导致的编译问题解决
- Linux下使用Eclipse编译C/C++代码时遇到问题的说明
- Linux 驱动开发之内核模块开发 (二)—— 内核模块编译 Makefile 入门
- 工作日志1:linux环境编译问题.
- Linux环境下不重新编译php添加扩展模块
- 部署linux系统下filezilla编译环境及其遇到的问题
- Linux编译模块的Makefile