本来打算写一个简单的demo去验证sipn_lock、mutex_lock死锁的,
结果代码写好了,忘了怎么编译成ko了...
还是自己手动记一下,方便以后找资料。
(有很多的博客其实主要内容都是别人的,自己只是照着做了一遍,然后把其中的试错都记录下来,防止下一次再用时再出错)
不废话,直接照做:
写一个.c代码
//下面是驱动源代码
#include <linux/init.h>
#include <linux/module.h>
static int hello_init(void)
{
printk("Hello, World !\n");
return 0;
}
static void hello_exit(void)
{
printk("Goodbye, World !\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("Dual BSD/GPL");
写一个Makefile文件
//下面是Makefile代码
#!/bin/bash
#通知编译器我们要编译模块的哪些源码
#这里是编译 hello.c这个文件编译成中间文件 hello.o
obj-m := hello.o
#声明当前的架构,这里用户需要根据实际情况选择架构类型
export ARCH=arm
#声明交叉编译工具链,这里用户需要根据实际情况选择对应的工具链
export CROSS_COMPILE=arm-himix200-linux-
#源码目录变量,这里用户需要根据实际情况选择路径
#下面作者是将Linux的源码目录
KERDIR := /home1/zhugeyifan/source/K5/3519av100/packages/linux_lsp/kernel/linux-4.9.37/
#当前目录变量
CURDIR := $(shell pwd)
#make命名默认寻找第一个目标
#make -C就是指调用执行的路径
#$(KERDIR)Linux源码目录,作者这里指的是.../linux-4.9.37/
#$(CURDIR)当前目录变量
#modules要执行的操作
#注意!假如复制到Makefile后,下行提示红色,把 all:下面一行的make前面的空格删除后添加(Tab)制表符
all:
make -C $(KERDIR) M=$(CURDIR) modules
#make clean执行的操作是删除后缀为o的文件
#注意!假如复制到Makefile后,下行提示红色,把 clean:下面一行的make前面的空格删除后添加(Tab)制表符
clean:
make -C $(KERDIR) M=$(CURDIR) clean
编译
直接执行make,编译生成ko模块
作者有遇到如下报错:
make -C /home1/zhugeyifan/source/K5/3519av100/packages/linux_lsp/kernel/linux-4.9.37/ M=/home1/zhugeyifan/source/K5/3519av100/packages/linux_lsp/kernel/test_ko/hello modules
make[1]: Entering directory `/home1/zhugeyifan/source/K5/3519av100/packages/linux_lsp/kernel/linux-4.9.37'
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 ./Module.symvers
is missing; modules will have no dependencies and modversions.
CC [M] /home1/zhugeyifan/source/K5/3519av100/packages/linux_lsp/kernel/test_ko/hello/hello.o
In file included from <command-line>:0:0:
././include/linux/kconfig.h:4:32: fatal error: generated/autoconf.h: No such file or directory
#include <generated/autoconf.h>
^
compilation terminated.
make[2]: *** [/home1/zhugeyifan/source/K5/3519av100/packages/linux_lsp/kernel/test_ko/hello/hello.o] Error 1
make[1]: *** [_module_/home1/zhugeyifan/source/K5/3519av100/packages/linux_lsp/kernel/test_ko/hello] Error 2
make[1]: Leaving directory `/home1/zhugeyifan/source/K5/3519av100/packages/linux_lsp/kernel/linux-4.9.37'
make: *** [all] Error 2
原因是内核没有编译,先处理内核编译
生成
编译成功后,生成文件如下:
zhugeyifan@DriverTeam:~/hello/ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile modules.order Module.symvers