How to compile the first LINUX module hello. C (centos5.3)

Source: Internet
Author: User

<! -- @ Page {margin: 2 cm} p {margin-bottom: 0.21} -->


<! -- @ Page {margin: 2 cm} p {margin-bottom: 0.21} -->


After reading the book, I did it step by step according to the method described in the book, but failed. 555
Actually, the author of the book does not consider the details.
Create a directory
[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> // header files required by all modules
# Include <Linux/init. h> // init & Exit macros
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. Download the Linux 2.6 kernel at www.kernel.org and decompress it to the/usr/src/linux26 directory.
[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
----------------------------------------------
Write a MAKEFILE file:
The content is as follows:
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. o
PC3: Error: Include/Linux/Autoconf. h: no such file or directory
In a file that contains include/Linux/posix_types.h: 47,
From include/Linux/types. h: 14,
From include/Linux/prefetch. h: 13,
From include/Linux/list. h: 8,
From include/Linux/module. h: 9,
From/home/Liu/test/hellomod. C: 1:
/Usr/lib/GCC/i486-linux-gnu/4.1.3/include/ASM/posix_types.h: 13: 22: Error: features. h: no such file or directory
/Usr/lib/GCC/i486-linux-gnu/4.1.3/include/ASM/posix_types.h: 14: 35: Error: No include path available for searching ASM/posix_types.h
...............................

Solution:

[Liu @ Liu-desktop hellomod] # Make oldconfig
[Liu @ Liu-desktop hellomod] # Make prepare

Now, try:
[Liu @ Liu-desktop hellomod] $ make-C/usr/src/linux26/subdirs = $ PWD modules
Still wrong:
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. o
Building modules, stage 2.
Modpost 1 modules
/Bin/sh: scripts/MoD/modpost: not found
Make [1]: *** [_ modpost] Error 127
Make: *** [modules] Error 2
Make: Leaving directory '/usr/src/linux26'

Did you see it? I am prompted that there is no scripts/MoD/modpost, so let's compile it.
[Root @ Liu-desktop linux26] # Make scripts
Hostcc scripts/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
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. Mod. o
LD [m]/home/Liu/test/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

Note: If the following error occurs, the kernel version 99% is incorrect, that is, the version magic is incorrect. <! -- @ Page {margin: 2 cm} p {margin-bottom: 0.21} -->


Insmod: Error inserting 'hellomod. Ko ':-1 invalid module format

In this case, you use sudo tail/var/log/messages
You should see a prompt similar to the following in the last line:
<! -- @ Page {margin: 2 cm} p {margin-bottom: 0.21} -->

Dec 19 13:42:29 localhost kernel: hellomod: Version magic '2. 6.24.2SMP mod_unload 686 4 kstacks 'should be '2. 6.27.7-134. fc10.i686SMP mod_unload 686 4 kstacks'

What should we do? The simplest way is to modify makefie in the source directory.
Set
Change the value of line 1-4 of the makefile to the same value as the current kernel.
Version = 2
Patchlevel = 6
Sublevel = 24
Extraversion =. 2
Name = err metey! A heury beelge-A ret!

How can we determine the value of your current kernel?

VI/lib/modules/'uname-R'/build/makefile
Do you know now?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.