首页 > 解决方案 > How to compile Linux kernel module without kernel Makefile?

问题描述

I have an ARM Linux device, but I don't have the Makefile of the kernel to build a kernel module.

I have GCC cross compiler to this arch.

How can I compile a kernel module without the Makefile of the kernel?

标签: linuxgcclinux-kernelcross-compilingkernel-module

解决方案


如何在没有内核 Makefile 的情况下编译内核模块?

你不能。您需要内核Makefile来编译模块,以及预构建的内核源代码树。在大多数发行版上,您可以通过包获取构建模块的内核源代码,例如linux-headers-xxxwhere xxxshould be output of uname -r.


例如,在 Debian 或 Ubuntu 上:

sudo apt-get install linux-headers-$(uanme -r)

然后,您将在 中找到构建模块所需的文件/lib/modules/$(uname -r)/build,并且您可以使用Makefile如下所示构建模块:

KDIR  := /lib/modules/$(shell uname -r)/build
PWD   := $(shell pwd)
obj-m := mymodule.o     # same name as the .c file of your module

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

这基本上是调用内核Makefile告诉它在当前目录中构建模块。


另外,我不确定你为什么说你在 ARM Linux 设备上,并且你有一个交叉编译器。如果您在设备本身上,则根本不需要交叉编译器。如果您在不同的设备上,那么您需要使用适合uname -r目标设备的设备才能获得正确的源并进行构建。您可能需要手动执行此操作,因为 的输出uname -r并不总是有帮助。

您还需要在模块中指定架构和交叉编译工具链前缀Makefile,例如:

default:
    $(MAKE) -C $(KDIR) M=$(PWD) ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- modules

推荐阅读