当前位置:   article > 正文

如何在Android中编译驱动模块 .ko_android gcc has been deprecated in favor of clang,

android gcc has been deprecated in favor of clang, and will be removed from

简介

调试驱动比较简单快捷的方式,是将驱动程序编译成模块,方便快速迭代修改/debug,但是编译如果每次都要make bootimages, 会非常影响开发速度,所以需要有一个相对快捷的方法来直接编译出模块。
  • 1

Linux方式

Makefile文件写法

obj-m = spi_mcu.o

KDIR := /lib/modules/`uname -r`/build

PWD := $(shell pwd)

all:
	$(MAKE) -C $(KDIR) M=$(PWD) modules

install:all
	$(MAKE) -C $(KDIR) M=$(PWD) modules_install

clean:
	$(MAKE) -C $(KDIR) M=$(PWD) clean
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

C文件写法

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

static int spi_mcu_init(void)
{
    return 0;
}

static void spi_mcu_exit(void)
{
}

module_init(spi_mcu_init);
module_exit(spi_mcu_exit);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

命令行直接make

$ make
make -C /lib/modules/`uname -r`/build M=spi_mcu modules
make[1]: Entering directory `/usr/src/linux-headers-4.4.0-142-generic'
  Building modules, stage 2.
  MODPOST 1 modules
make[1]: Leaving directory `/usr/src/linux-headers-4.4.0-142-generic'
$ file spi_mcu.ko 
spi_mcu.ko: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), BuildID[sha1]=d9a1f37ba99e329384a87b9cb699ed8c1388cc7e, not stripped
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

生成的是一个X86-64的驱动,适合在pc上安装,不是在Android/ARM设备上安装,下面我们看下arm的方式。

Android方式

obj-m = spi_mcu.o

KDIR := ${OUT}/obj/KERNEL_OBJ/

PWD := $(shell pwd)

all:
	$(MAKE) -C $(KDIR) M=$(PWD) ARCH=arm64 $(KDIR).config modules

install:all
	$(MAKE) -C $(KDIR) M=$(PWD) modules_install

clean:
	$(MAKE) -C $(KDIR) M=$(PWD) clean
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

将Makefile稍加修改即可,编译得到64位的驱动模块

$ file spi_mcu.ko 
spi_mcu.ko: ELF 64-bit LSB relocatable, ARM aarch64, version 1 (SYSV), BuildID[sha1]=26d68644cfba0bcf91b3af970c8b7b11c4048029, not stripped
  • 1
  • 2

问题

编译提示以下问题,google准备弃用GPL的gcc,采用BSD授权的clang/LLVM,要试一下编译的ko是否可以正常工作。

Android GCC has been deprecated in favor of Clang, and will be removed from
Android in 2020-01 as per the deprecation plan in:
https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/+/master/GCC_4_9_DEPRECATION.md
  • 1
  • 2
  • 3

结果

在板子上可以正确insmod,debug阶段这样比较快速,集成到系统里面可以直接放入到kernel当中编译

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/311709
推荐阅读
相关标签
  

闽ICP备14008679号