【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权)
Linux 编译内核模块的通用Makefile
KERN_DIR := xxxxx/linux-4.9.y-smp
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += led_drv.o
KERN_DIR 指向Linux内核路径;
obj-m 后面加的是驱动文件的对象文件(你应该懂的)
你有可能出现的问题, 当然问题不在Makefile上:
..... 上面的忽略, 毕竟太长了 .....
..... 上面的忽略, 毕竟太长了 .....
..... 上面的忽略, 毕竟太长了 .....
./include/linux/thread_info.h:108:22: error: implicit declaration of function ‘current_thread_info’; did you mean ‘current_text_addr’? [-Werror=implicit-function-declaration]
test_ti_thread_flag(current_thread_info(), flag)
^
./arch/x86/include/asm/elf.h:349:3: note: in expansion of macro ‘test_thread_flag’
test_thread_flag(TIF_ADDR32));
^~~~~~~~~~~~~~~~
./include/linux/thread_info.h:108:22: warning: passing argument 1 of ‘test_ti_thread_flag’ makes pointer from integer without a cast [-Wint-conversion]
test_ti_thread_flag(current_thread_info(), flag)
^
./arch/x86/include/asm/elf.h:349:3: note: in expansion of macro ‘test_thread_flag’
test_thread_flag(TIF_ADDR32));
^~~~~~~~~~~~~~~~
./include/linux/thread_info.h:94:19: note: expected ‘struct thread_info *’ but argument is of type ‘int’
static inline int test_ti_thread_flag(struct thread_info *ti, int flag)
^~~~~~~~~~~~~~~~~~~
In file included from ./include/linux/module.h:25:0,
from xxxxx/led_cntl/led_drv.c:2:
./arch/x86/include/asm/module.h: At top level:
./arch/x86/include/asm/module.h:57:2: error: #error unknown processor family
#error unknown processor family
^~~~~
In file included from xxxxx/led_cntl/led_drv.c:2:0:
./include/linux/module.h:400:27: error: field ‘arch’ has incomplete type
struct mod_arch_specific arch;
^~~~
cc1: some warnings being treated as errors
scripts/Makefile.build:299: recipe for target 'xxxxx/led_cntl/led_drv.o' failed
make[2]: *** [xxxxx/led_cntl/led_drv.o] Error 1
Makefile:1490: recipe for target '_module_xxxxx/led_cntl' failed
make[1]: *** [_module_xxxxx/led_cntl] Error 2
make[1]: Leaving directory 'xxxxx/osdrv/opensource/kernel/linux-4.9.y-smp'
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 2
为什么会这样的?
答: 是因为我们没有指定make时使用的架构和编译链. 我们需要在Makefile中补上.
当我编译内核时, 我使用的是如下的命令:
make ARCH=arm CROSS_COMPILE=arm-himix200-linux- uImage
而我的Makefile中却没有加上ARCH和CROSS_COMPILE参数, 故把Makefile改成如下即可:
KERN_DIR := xxxxx/linux-4.9.y-smp
all:
make ARCH=arm CROSS_COMPILE=arm-himix200-linux- -C $(KERN_DIR) M=`pwd` modules
clean:
make ARCH=arm CROSS_COMPILE=arm-himix200-linux- -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += led_drv.o