/******************************************************
*帅得不敢出门 C++爱好者灌水天堂 3503799 转载请保留此信息
******************************************************/
由于没有开发板,只好做pc机下linux的模块加载了.原理与开发板的是一样的.
编写dri_arh.c源代码
#include<linux/string.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/init.h>
#include<linux/types.h>
static int __init dri_arch_init_module(void)
{
printk("This is a simple driver-module!/r/n");
return 0;
}
static void __exit dri_arch_cleanup_module(void)
{
printk("Goodbye driver-module!/r/n");
}
module_init(dri_arch_init_module);
module_exit(dri_arch_cleanup_module);
然后编译,我就不写Makefile了 直接命令编译它
[root@localhost 0430]# gcc -D__KERNEL__ -DMODULE -Wall -c -O2 -o dri_arch.o
dri_arch.c
In file included from dri_arch.c:1:
/usr/include/linux/string.h:8:2: warning: #warning Using kernel header in
userland!
dri_arch.c: In function `dri_arch_init_module':
dri_arch.c:9: warning: implicit declaration of function `printk'
出现错误了,头文件把它包含进去就行了
[root@localhost 0430]# gcc -D__KERNEL__ -DMODULE -Wall
-I/usr/src/linux-2.4.20-8/include -c -O2 -o dri_arch.o
dri_arch.c
生成了.o文件了. 加载到内核中去(原先我加载过了,先rmmod把它卸载掉)
[root@localhost 0430]# insmod dri_arch.o
dri_arch.o: kernel-module version mismatch
dri_arch.o was compiled for kernel version 2.4.20-8custom
while this kernel is version 2.4.20-8.
发现出现系统模块版本不匹配
于是make menuconfig
make
编译内核,再重新编译模块,并加载,还是不行,只好修改/usr/src/linux-2.4.20-8/include/linux/versions.h
其原先内容如下:
#define UTS_RELEASE "2.4.20-8custom"
#define LINUX_VERSION_CODE 132116
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
把其中的custom去掉保存.
再编译加载
[root@localhost 0430]# gcc -D__KERNEL__ -DMODULE -Wall -O2
-I/usr/src/linux-2.4.20-8/include -c -o dri_arch.o dri_arch.c
[root@localhost 0430]# insmod dri_arch.o
Warning: loading dri_arch.o will taint the kernel: no license
See
http://www.tux.org/lkml/#export-tainted for information about tainted
modules
Module dri_arch loaded, with warnings
加载成功了.
$lsmod (查看当前已挂载模块,会看到dri_arch)
$dmesg (查看模块输出信息:This is a simple driver-module!)
$rmmod dri_arch (卸载dri_arch)
$lsmod (查看当前已挂载模块,不再看到dri_arch)
$dmesg (查看模块输出信息:Goodbye driver-module!)
实验结束! 谢谢