Linux驱动模块编译模板
hello.c文件:
#include <linux/module.h> #include <linux/kernel.h> static int hello_init(void) { printk("Hello,world!\n"); return 0; } static void hello_exit(void) { printk("Goodbye,cruel world!\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_AUTHOR("Vedic <FZKmxcz@163.com>"); MODULE_LICENSE("Dual BSD/GPL");
Makefile文件:
obj-m +=template.o template-objs:= hello.o KDIR:=/home/fuzk/project/Prolin/firmware_4/build_dir/linux-sc9820_sc9820_pax/linux-3.10.65 COMPILER=/opt/toolchain/arm-2012.03/bin/arm-none-linux-gnueabi-gcc LD_PATH=/opt/toolchain/arm-2012.03/bin/arm-none-linux-gnueabi-ld AR_PATH=/opt/toolchain/arm-2012.03/bin/arm-none-linux-gnueabi-ar ARCH_TYPE=arm
#CCFLAGS新GGC要求改用ccflags-y, -I头文件必须是绝对路径 像-I./include压根找不到 宏定义用-D紧跟着宏名字即可不用空格 #ccflags-y += -mfloat-abi=softfp -Ixx/xx/include -DXXX_RRR #ldflags-y += -L/opt/toolchain/arm-2012.03/lib/gcc/arm-none-linux-gnueabi/4.6.3 -lgcc -static all: make CC=$(COMPILER) LD=$(LD_PATH) AR=$(AR_PATH) ARCH=$(ARCH_TYPE) -C $(KDIR) M=$(PWD) modules clean: make CC=$(COMPILER) LD=$(LD_PATH) ARCH=$(ARCH_TYPE) -C $(KDIR) M=$(PWD) clean
追加:
上面的Makefile其实写得不严谨!首先make命令后处理的Makefile是kernel下的Makefile, 查看内容得知里面除了用CC LD AR 还会用其他的
所以我们要做的是赋值CROSS_COMPILE变量才对
obj-m +=template.o template-objs:= hello.o KDIR:=/home/fuzk/project/Prolin/firmware_4/build_dir/linux-sc9820_sc9820_pax/linux-3.10.65 COMPILE=/opt/toolchain/arm-2012.03/bin/arm-none-linux-gnueabi- ARCH_TYPE=arm #CCFLAGS新GGC要求改用ccflags-y, -I头文件必须是绝对路径 像-I./include压根找不到 宏定义用-D紧跟着宏名字即可不用空格 #ccflags-y += -mfloat-abi=softfp -Ixx/xx/include -DXXX_RRR #ldflags-y += -L/opt/toolchain/arm-2012.03/lib/gcc/arm-none-linux-gnueabi/4.6.3 -lgcc -static all: make CROSS_COMPILE=$(COMPILE) ARCH=$(ARCH_TYPE) -C $(KDIR) M=$(PWD) modules clean: make CROSS_COMPILE=$(COMPILE) ARCH=$(ARCH_TYPE) -C $(KDIR) M=$(PWD) clean