第一次编译模块总结

来源:互联网 发布:剑雨江湖602进阶数据 编辑:程序博客网 时间:2024/06/05 00:40
  

1:模块代码:.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
 printk(KERN_ALERT,"load hello module");
return 0;
}
static void hello_exit(void)
{
 printk(KERN_ALERT,"unload hello module");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_ALIAS("mhwgo-hello");

2:makefile文件:(只有一句)
obj-m :=helloworld.o

3:执行:
sudo make -C /usr/src/linux-headers-2.6.28-16/ M=$(pwd) modules

4:
[root@liu-desktop linux26]#insmod hellomod.ko
[root@liu-desktop linux26]#lsmod |grep hellomod
lsmod |grep hellomod
[root@liu-desktop linux26]#rmmod hellomod
注意:如果出现下面错误,那应该是内核版本号对不上,version magic不对
insmod: error inserting 'hellomod.ko': -1 Invalid module format
此时,你用sudo tail /var/log/messages
你在最后一行应该看到类似下面的提示:
Dec 19 13:42:29 localhost kernel: hellomod: version magic '2.6.24.2 SMP mod_unload 686 4KSTACKS ' should be '2.6.27.7-134.fc10.i686 SMP mod_unload 686 4KSTACKS '
总结:
1:
进行模块编译前,最好下载一个完整的内核源码(不一定需要最新的),并对其进行编译。
编译所需的.config文件可以从系统中直接拷(如命令:cp /boot/config-2.6.28-16 .config)
2:
可能会用到的命令如下:
sudo apt-get install build-essential
sudo apt-get install libncurses*
 
问题1:
ERROR: Kernel configuration is invalid.
include/linux/autoconf.h or include/config/auto.conf are missing.
Run 'make oldconfig && make prepare' on kernel src to fix it.
make oldconfig && make prepare  这两条命令的作用?
 
问题2:
执行:sudo make prepare
出现错误:
scripts/kconfig/conf -s arch/x86/Kconfig
  CHK     include/linux/version.h
  UPD     include/linux/version.h
  CHK     include/linux/utsrelease.h
  UPD     include/linux/utsrelease.h
  SYMLINK include/asm -> include/asm-x86
make[1]: *** No rule to make target `kernel/bounds.c', needed by `kernel/bounds.s'.  Stop.
make: *** [prepare0] Error 2
什么原因?
可能原因:由于缺少文件
解决办法:重新下载一个内核源程序,先把内核编译一遍,再来对模块进行编译。
 

问题3:
sudo apt-get install build-essential
or
sudo apt-get install build-essential linux-headers-$(uname -r)
正在读取软件包列表... 完成
正在分析软件包的依赖关系树      
正在读取状态信息... 完成      
现在没有可用的软件包 build-essential,但是它被其它的软件包引用了。
这可能意味着这个缺失的软件包可能已被废弃,
或者只能在其他发布源中找到
E: 软件包 build-essential 还没有可供安装的候选者
为什么失败?
解决:软件源列表不齐全,可以更新软件源。
问题4:
sudo apt-get install linux-source
sudo apt-get install glibc-source
作用?
问题5:
  *** Unable to find the ncurses libraries or the
*** required header files.
*** 'make menuconfig' requires the ncurses libraries.
***
*** Install ncurses (ncurses-devel) and try again.
***
make[1]: *** [scripts/kconfig/dochecklxdialog] Error 1
make: *** [menuconfig] Error 2
这是什么问题?
 原因:解决:软件源列表不齐全,可以更新软件源。