这也是一时心血来潮,觉得好玩。 在Document/kbuild/modules.txt中看到这么一段
想着好玩,就自己试了一下,开始不成功,后来在高手的帮助下终于成功了。
1。 创建自己的obj文件
我最开始使用 gcc -c ex_obj.c -o ex_obj.o_shipped 来做的,结果链接后不能正常工作。
原来是一定要放在编译kernel的环境中编译这个obj。网上高手说了 要这么写
ifneq ($(KERNELRELEASE),)
obj-y := xxxx.o #your obj files goes here. 关键在这里,用obj-y 来指定要编译的,然后这个过程就在编译的时候停了,不会继续链接。
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD)
endif
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
depend .depend dep:
$(CC) $(CFLAGS) -M *.c > .depend
ifeq (.depend,$(wildcard .depend))
include .depend
endif
编译成功后,将这个拷贝到需要链接的模块目录。
记住要添加_shipped后缀
2。 然后在需要这个obj的模块中添加
scull-y := main.o pipe.o access.o ex_obj.o
然后在源代码中调用相应的函数,ok,可以了。
不过貌似没有啥实际用处。 :)