无法在Android JellyBean上构buildHello World核心模块

我正在尝试在Android JellyBean上构build一个简单的内核模块。

码:

#include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_ALERT */ MODULE_LICENSE("GPL"); MODULE_AUTHOR("test"); MODULE_DESCRIPTION("Android ko test"); int init_module(void) { printk(KERN_ALERT, "Hello world\n"); // A non 0 return means init_module failed; module can't be loaded. return 0; } void cleanup_module(void) { printk(KERN_ALERT "Goodbye world 1.\n"); } 

Makefile文件:

 obj-m +=hello.o KERNELDIR ?= ~/android/kernel PWD := $(shell pwd) CROSS_COMPILE=~/android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- ARCH=arm default: $(MAKE) -C $(KERNELDIR) M=$(PWD) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) modules clean: $(MAKE) -C $(KERNELDIR) M=$(PWD) clean 

输出:

 make -C ~/android/kernel M=/home/test/testmod ARCH=arm CROSS_COMPILE=~/android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- modules make[1]: Entering directory `/home/test/android/kernel' ERROR: Kernel configuration is invalid. include/generated/autoconf.h or include/config/auto.conf are missing. Run 'make oldconfig && make prepare' on kernel src to fix it. WARNING: Symbol version dump /home/test/android/kernel/Module.symvers is missing; modules will have no dependencies and modversions. CC [M] /home/test/testmod/hello.o In file included from <command-line>:0: /home/test/android/kernel/include/linux/kconfig.h:4:32: error: generated/autoconf.h: No such file or directory In file included from /home/test/android/kernel/arch/arm/include/asm/types.h:4, from include/linux/types.h:4, from include/linux/list.h:4, from include/linux/module.h:9, from /home/test/testmod/hello.c:1: include/asm-generic/int-ll64.h:11:29: error: asm/bitsperlong.h: No such file or directory In file included from /home/test/android/kernel/arch/arm/include/asm/posix_types.h:38, from include/linux/posix_types.h:47, from include/linux/types.h:17, from include/linux/list.h:4, from include/linux/module.h:9, from /home/test/testmod/hello.c:1: include/asm-generic/posix_types.h:70:5: warning: "__BITS_PER_LONG" is not defined error, forbidden warning: posix_types.h:70 make[2]: *** [/home/test/testmod/hello.o] Error 1 make[1]: *** [_module_/home/test/testmod] Error 2 make[1]: Leaving directory `/home/test/android/kernel' make: *** [default] Error 2 

如果我按照输出中的build议,并在内核上运行“make oldconfig && make prepare”,它会引导我通过几十个内核configuration是/否问题。 之后,编译仍然失败的下一个错误,这是关于bitsperlong.h。

Solutions Collecting From Web of "无法在Android JellyBean上构buildHello World核心模块"

Android将输出二进制文件放在out目录下。 例如,如果定义了$ANDROID_PRODUCT_OUT那么可以使用out/target/product/<target name>/obj/KERNEL_OBJ/$ANDROID_PRODUCT_OUT/obj/KERNEL_OBJ/ 。 这个目录可能与不同的供应商有不同的名称,但这只是包含vmlinux的目录。

所以当你在Android repo下编译一个内核模块时,你应该在模块的目录下面提交如下的make命令。

 make -C $ANDROID_PRODUCT_OUT/obj/KERNEL_OBJ/ M=`pwd` ARCH=arm CROSS_COMPILE=arm-eabi- modules 

首先确保你已经在指定的路径中编译了内核。 那是

 " /home/test/android/kernel" but you are using " /home/android/kernel " during compilation of module KERNELDIR ?= ~/android/kernel has to be KERNELDIR ?= ~/test/android/kernel 

如果没有,那么在~/android/kernel目录下运行下面的命令来编译内核。

 make ARCH=arm CROSS_COMPILE=~/test/android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi 

内核编译完成后,你会得到这个在内核i的System.map文件中定义的“__BITS_PER_LONG”变量,e

 ~/test/android/kernel/System.map 

在此之后,您将能够编译您的模块,而不会有任何障碍

make模块应该在编译内核至少一次之后完成。 你还没有编译内核,这就是为什么modulee.symvers丢失。 在编译期间,会创建一些头文件,如asm / bitsperlong.h。