编译一个linux内核模块

看源代码。

再来看 makefile的写法

TARGET = helloOBJS = hello.oMDIR = drivers/misc

EXTRA_CFLAGS = -DEXPORT_SYMTABCURRENT = $(shell uname -r)KDIR = /lib/modules/$(CURRENT)/buildPWD = $(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 -a”elseinstall:su -c “cp -v $(TARGET).ko $(DEST) && /sbin/depmod -a”endif

clean:-rm -f *.o *.ko .*.cmd .*.flags *.mod.c

-include $(KDIR)/Rules.make

好了 放入到位置运行makefile即可

插入模块的命令是这样的

$insmod hello.ko(PS:尽管你看不到有.ko这样的文件,但是的确是这么来做的)

用 dmesg 就可以看到 输出啦。。。。

补充一个错误:

./linux/include/asm/elf.h: In function `start_ia32_thread’:./linux/include/asm/elf.h:153: warning: implicit declaration of function `load_gs_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 included from ./linux/include/linux/module.h:21, from hello.c:2:./linux/include/asm/module.h:70:2: #error unknown processor family

可能路径显示不是这样 因为我是复制出来源代码来编译的,但是错误的确是这样。其实这个错误是makefile的问题,因为用

-Wall -DMODULE -D__KERNEL__ -D__SMT__ 的编译方法在kenel2.6下不行的。所以 按照上面写的makefile 就好了。。

人的一生是奋斗的一生,人们为了取得成功都在不断地努力着,

编译一个linux内核模块

相关文章:

你感兴趣的文章:

标签云: