编译hello.ko到目标板运行

1

目标板为手机,有现成的pac包可以正常的运行

开发环境为linux,在linux源码下添加test目录, hello.c Makefile

 

2hello.c内容

#include<linux/init.h>
#include <linux/module.h>

staticint __init hello_init(void)
{

rintk( "Helloword\n");
        return 0;
}

staticvoid  __exit hello_exit(void)
{

 printk( "Goodbye, wordn");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("DualBSD/GPL");

 

3Makefile内容

ifneq($(KERNELRELEASE),)
obj-m += hello.o
hello-objs += hello.o
hello -objs += xx.o
else
KDIR := /home6/night/marlin2/kernel/
all:

 make -C $(KDIR) M=$(PWD) modules
clean:

 rm -f *.ko *.o *.mod.o *.mod.c *.symvers
endif

Makefile解析:在test目录执行make的时候,KERNELRELEASE是没有定义的,所以就走else路线,KDIR定义了linux src的起始路径,里面包含了Makefile,执行第一个命令all make –C 指定调用KDIRmakefileM=$(PWD) 表明然后返回到当前目录继续读入、执行当前的Makefile。当从内核源码目录返回时,KERNELRELEASE已被定义,kbuild也被启动去解析kbuild语法的语句,make将继续读取else之前的内容,生成hello.ko目标文件,并编译其依赖文件hello.cxx.c. “M=”选项的作用是,当用户需要以某个内核为基础编译一个外部模块的话,需要在makemodules 命令中加入“M=dir”,程序会自动到你所指定的dir目录中查找模块源码,将其编译,生成KO文件。

 

4、在test目录执行make命令

得到错误

4.1  include/linux/autoconf.h orinclude/config/auto.conf are missing. Run 'make oldconfig && makeprepare' on kernel src to fix it.

à这主要是因为内核没有被编译过,或者编译之后相关的一些配置文件被删除,查看src目录是否有.config配置文件,出现这种现象一般没有,可直接cparch/arm/configs/xxx_defconfig ./.config,然后执行make prepare

4.2 生成ko之后,将其push到手机,adb push/system/lib/modules/ insmod hello.ko出现

insmod: init_module'hello.ko' failed (Exec format error),暂时未能解决


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值