Compile a Linux kernel module

Source: Internet
Author: User

Check the source code.

 

# Include <Linux/kernel. h> <br/> # include <Linux/module. h> </P> <p> # If config_modversions = 1 <br/> # define modversions <br/> # include <Linux/modversions. h> <br/> # endif </P> <p> int init_module () <br/> {<br/> printk ("hello, i'm kernel/N "); <br/> return 0; <br/>}</P> <p> void cleanup_module () <br/>{< br/> printk ("I'm kernel, bye/N"); <br/>}< br/>

 

Let's take a look at makefile writing.

 

Target = Hello
Objs = Hello. o
Mdir = Drivers/Misc

Extra_cflags =-dexport_symtab
Current = $ (shell uname-R)
Kdir =/lib/modules/$ (current)/build
Pwd = $ (shell PWD)
DeST =/lib/modules/$ (current)/kernel/$ (mdir)

OBJ-M: = $ (target). o

Default:
Make-C $ (kdir) subdirs = $ (PWD) Modules

$ (Target). O: $ (objs)
$ (LD) $ (ld_rflag)-R-o $ @ $ (objs)

Ifneq (, $ (findstring 2.4., $ (current )))
Install:
Echo $ (DEST) $ (ld_rflag)
Su-c "CP-V $ (target). o $ (DEST) &/sbin/depmod-"
Else
Install:
Su-c "CP-V $ (target). Ko $ (DEST) &/sbin/depmod-"
Endif

Clean:
-Rm-f *. O *. KO. *. cmd. *. Flags *. Mod. c

-Include $ (kdir)/rules. Make

 

Run makefile at the location.

 

The command for inserting a module is as follows:

 

$ Insmod hello. Ko (PS: although you can't see a file like. Ko, it does)

 

Dmesg can be used to view the output ....

 

Add an error:

 

 

./Linux/include/ASM/elf. H: In function 'start _ ia32_thread ':
./Linux/include/ASM/elf. h: 153: Warning: Implicit declaration of function 'Load _ g
S_index'
./Linux/include/ASM/elf. H: In function 'elf _ common_init ':
./Linux/include/ASM/elf. h: 166: Error: structure has no member named 'r8'
./Linux/include/ASM/elf. h: 166: Error: structure has no member named 'r9'
./Linux/include/ASM/elf. h: 166: Error: structure has no member named 'r10'
./Linux/include/ASM/elf. h: 166: Error: structure has no member named 'r11'
./Linux/include/ASM/elf. h: 167: Error: structure has no member named 'r12'
./Linux/include/ASM/elf. h: 167: Error: structure has no member named 'r13'
./Linux/include/ASM/elf. h: 167: Error: structure has no member named 'r14'
./Linux/include/ASM/elf. h: 167: Error: structure has no member named 'r15'
In file encoded ded from./Linux/include/Linux/module. h: 21,
From hello. C: 2:
./Linux/include/ASM/module. h: 70: 2: # error unknown Processor family

 

The path may not be displayed because I copied the source code for compilation, but the error is true. In fact, this error is caused by makefile because

-The wall-dmodule-d1_kernel _-d1_smt _ compilation method cannot be used in kenel2.6. So follow the makefile written above ..

 

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.