如何編譯linux第一個模組 hello.c

來源:互聯網
上載者:User

看了書後,照著書上的方法一步一步去做,卻失敗了,555
真是的,寫書的人啊,卻不考慮一下細節問題
建立一個目錄


[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不對

p { margin-bottom: 0.21cm; }


insmod:
error inserting 'hellomod.ko': -1 Invalid module format


此時,你用sudo tail /var/log/messages
你在最後一行應該看到類似下面的提示:

p { margin-bottom: 0.21cm; }

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
現在知道了吧?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.