如何編譯第一個模組hello

來源:互聯網
上載者:User

建立一個目錄
[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
現在知道了吧?

補充:

insmod: error inserting 'hellomod.ko': -1 Invalid module format
sudo tail /var/log/messages
no symbol version for module_layout

。。。

發現編譯驅動時有個警告:

  WARNING: Symbol version dump /home/ftp/linux-2.6.32.21/Module.symvers
           is missing; modules will have no dependencies and modversions.

少Module.symvers 這個檔案, 百度了一下這個檔案在編譯核心後產生的。

知道原因了,那麼就應該知道怎麼解決了。

 

可能還會遇到這種問題:

disagrees about version of symbol module_layout,詳細看這裡。

http://www.ibm.com/developerworks/cn/linux/l-cn-kernelmodules/index.html

在X86上我的辦法是:

我的系統是ubuntu 10.04LTS

make -C /usr/src/linux-headers-2.6.32-21-generic SUBDIRS=$PWD modules

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.