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 ..