如何编译linux第一个模块 hello.c

看了书后,照着书上的方法一步一步去做,却失败了,555555555555555
真是的,写书的人啊,却不考虑一下细节问题
新建一个目录
[liu@liu-desktop hellomod] $mddir hellomod
[liu@liu-desktop hellomod] $cd hellomod
[liu@liu-desktop hellomod] $vi hellomod.c

/****************hellomod.c*******************************/
#include //所有模块都需要的头文件
#include // 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


[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

你可能感兴趣的:(如何编译linux第一个模块 hello.c)