本文内容适用于编译运行于本机的内核模块。

开发内核模块往往第一步的编译环境设置最容易出错,需要安装的主要有build-essential, 对应版本的linux-header。

然后就是Makefile的写法

1 obj-m := hello.o
2 # 网上很多是自己下载源码,然后用的是
3 # KDIR :=/usr/src/kernel-source-2.6.8类似的路径,这样很容易出错,造成
4 # 编译出来的内核版本号和实际运行的内核版本号不一致。在insmod时就会得到这
5 # 个错误 insmod: error inserting '': -1 Invalid module format
6 KDIR :=/lib/modules/$(shell uname -r)/build
7 PWD := $(shell pwd)
8 default:
9 $(MAKE) -C $(KDIR) M=$(PWD) modules

根据这个Makefile,直接make,就能正确编译。

最准确的,见The Linux Kernel Module Programming Guide:2.2. Compiling Kernel Modules

 

其他参考:

 

How to Create, Compile, Load Linux LKM Loadable Kernel Modules

Writing device drivers in Linux: A brief tutorial 

我第一个字符设备驱动程序的痛苦经历

如何编译linux第一个模块 hello.c

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-19
  • 2021-09-01
  • 2021-09-05
  • 2022-12-23
  • 2021-08-23
猜你喜欢
  • 2021-12-23
  • 2021-10-15
  • 2021-12-26
  • 2021-11-23
  • 2021-12-24
  • 2021-10-29
相关资源
相似解决方案