编译helloworld驱动

编译helloworld驱动


新建一个目录
[liu@liu-desktop hellomod]$mddir hellomod
[liu@liu-desktop hellomod]$cd hellomod
[liu@liu-desktop hellomod]$vi hellomod.c
/****************hellomod.c*******************************/
#include <linux/module.h> //所有模块都需要的头文件
#include <linux/init.h> // init&exit相关宏
MODULE_LICENSE("GPL");
static int __init hello_init (void)
{
    printk("Hello china init\n");
    return 0;
}
static void __exit hello_exit (void)
{
    printk("Hello china exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
/****************hellomod.c*******************************/

1、在www.kernel.org下载了linux 2.6的内核,解压到/usr/src/linux26目录下
[root@liu-desktop linux26]# ls
arch     CREDITS        drivers  init    kernel       Makefile  README          security
block    crypto         fs       ipc     lib          mm        REPORTING-BUGS  sound
COPYING  Documentation  include  Kbuild  MAINTAINERS  net       scripts         usr
----------------------------------------------
写一个Makefile文件:
内容如下:
obj-m := hellomod.o
------------------------------------------------
[liu@liu-desktop hellomod]$ make -C /usr/src/linux26/ SUBDIRS=$PWD modules
make: Entering directory `/usr/src/linux26'
  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.

  WARNING: Symbol version dump /usr/src/linux26/Module.symvers
           is missing; modules will have no dependencies and modversions.
  CC [M]  /home/liu/test/hellomod/hellomod.o
cc1: 错误: include/linux/autoconf.h:No such file or directory
在包含自 include/linux/posix_types.h:47 的文件中,
                 从 include/linux/types.h:14,
                 从 include/linux/prefetch.h:13,
                 从 include/linux/list.h:8,
                 从 include/linux/module.h:9,
                 从 /home/liu/test/hellomod/hellomod.c:1:
/usr/lib/gcc/i486-linux-gnu/4.1.3/include/asm/posix_types.h:13:22: 错误: features.h:No such file or directory
/usr/lib/gcc/i486-linux-gnu/4.1.3/include/asm/posix_types.h:14:35: 错误: 没有包含路径可供搜索 asm/posix_types.h
...............................

解决方法:
[liu@liu-desktop hellomod]#make oldconfig
[liu@liu-desktop hellomod]#make prepare

好了,在试试:
[liu@liu-desktop hellomod]$ make -C /usr/src/linux26/ SUBDIRS=$PWD modules
还是有错:
make: Entering directory `/usr/src/linux26'
  WARNING: Symbol version dump /usr/src/linux26/Module.symvers
           is missing; modules will have no dependencies and modversions.
  CC [M]  /home/liu/test/hellomod/hellomod.o
  Building modules, stage 2.
  MODPOST 1 modules
/bin/sh: scripts/mod/modpost: not found
make[1]: *** [__modpost] 错误 127
make: *** [modules] 错误 2
make: Leaving directory `/usr/src/linux26'

看到了吗,提示说没有scripts/mod/modpost,那我们就编译它吧
[root@liu-desktop linux26]# make scripts
  HOSTCC  scripts/genksyms/genksyms.o
  SHIPPED scripts/genksyms/lex.c
  SHIPPED scripts/genksyms/parse.h
  SHIPPED scripts/genksyms/keywords.c
  HOSTCC  scripts/genksyms/lex.o
  SHIPPED scripts/genksyms/parse.c
 HOSTCC  scripts/genksyms/parse.o
  HOSTLD  scripts/genksyms/genksyms
  CC      scripts/mod/empty.o
  HOSTCC  scripts/mod/mk_elfconfig
  MKELF   scripts/mod/elfconfig.h
  HOSTCC  scripts/mod/file2alias.o
  HOSTCC  scripts/mod/modpost.o
  HOSTCC  scripts/mod/sumversion.o
  HOSTLD  scripts/mod/modpost
  HOSTCC  scripts/kallsyms
  HOSTCC  scripts/conmakehash
OK,好了
[liu@liu-desktop hellomod]$ make -C /usr/src/linux26/ SUBDIRS=$PWD modules
make: Entering directory `/usr/src/linux26'
  WARNING: Symbol version dump /usr/src/linux26/Module.symvers
           is missing; modules will have no dependencies and modversions.
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/liu/test/hellomod/hellomod.mod.o
  LD [M]  /home/liu/test/hellomod/hellomod.ko
make: Leaving directory `/usr/src/linux26'
[liu@liu-desktop hellomod]$ ls
hellomod.c  hellomod.ko  hellomod.mod.c  hellomod.mod.o  hellomod.o  Makefile  Module.symvers

[root@liu-desktop linux26]#insmod hellomod.ko
[root@liu-desktop linux26]#lsmod |grep hellomod
lsmod |grep hellomod
[root@liu-desktop linux26]#rmmod hellomod

注意:如果出现下面错误,那99%是内核版本号对不上,也就是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 '

那该怎么办呢?最简单的办法就是:修改源目录下的Makefie

最Makefile第1-4行的值改为当前内核一样的值
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 24
EXTRAVERSION = .2
NAME = Err Metey! A Heury Beelge-a Ret!
那怎么确定你当前内核的值是多少呢?
vi  /lib/modules/`uname -r`/build/Makefile
现在知道了吧?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值