在Linux下编译内核模块的Makefile的简单写法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013554213/article/details/81050036

1 Makefile

新建文件,命名为Makefile,将下面的代码拷贝到文件中

#!/bin/bash

obj-m += test-demo.o 

#CROSS_COMPILE	?= /opt/linaro/gcc-linaro-5.3-2016.02-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-	# Compiler path settings

KERNELDIR :=/usr/src/kernel/kernel-4.4	# Kernel code directory

PWD ?= $(shell pwd)

all:
	make -C $(KERNELDIR) M=$(PWD) modules	# Compile the instructions of the kernel module

clean:                                              
	rm -rf *.ko *.mod.c *.o modules.* Module.symvers	# delete all generated files

将生成的Makefile文件复制到Linux系统下的某路径下,使其与 test-demo.c保存在同一路径下。
打开终端,进入上述路径,输入 make 命令,即可编译成对应的内核模块

 

2 Linux下.ko驱动模块管理

2.1 insmod加载驱动模块

insmod <module_name> .ko

2.2 lsmod查询驱动模块

lsmod

2.3 rmmod卸载驱动模块

rmmod <module_name> 

猜你喜欢

转载自blog.csdn.net/u013554213/article/details/81050036