简体   繁体   English

如何在没有 kernel ZB67911656EF5D18C65AE36CB67Z1B7 的情况下编译 Linux kernel 模块?

[英]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 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.我有 GCC 交叉编译器到这个拱门。

How can I compile a kernel module without the Makefile of the kernel?如何在没有Makefile的 Makefile 的情况下编译 kernel 模块?

How can I compile kernel module without have the Makefile of the kernel?在没有 kernel 的 Makefile 的情况下,如何编译 kernel 模块?

You can't.你不能。 You need the kernel Makefile in order to compile a module, along with a pre-built kernel source tree.您需要 kernel Makefile以编译模块,以及预构建的 kernel 源代码树。 On most distributions, you can obtain the kernel source for building modules through packages like linux-headers-xxx where xxx should be the output of uname -r .在大多数发行版中,您可以获得 kernel 源代码,用于通过linux-headers-xxx等软件包构建模块,其中xxx应该是uname -r的 output 。


For example, on Debian or Ubuntu:例如,在 Debian 或 Ubuntu 上:

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

You will then find the files needed for building modules at /lib/modules/$(uname -r)/build , and you can build a module with a Makefile like this:然后,您将在/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

What this does is basically invoking the kernel Makefile telling it to build the module in the current directory.这基本上是调用 kernel Makefile告诉它在当前目录中构建模块。


Also, I am not sure why you say that you are on an ARM Linux device, and you have a cross compiler.另外,我不确定您为什么说您使用的是 ARM Linux 设备,并且您有一个交叉编译器。 If you are on the device itself, you shouldn't need a cross compiler at all.如果您在设备本身上,则根本不需要交叉编译器。 If you are on a different device, then you'll need to use the appropriate uname -r for the target device in order to get the right sources and in order to build.如果您在不同的设备上,则需要为目标设备使用适当的uname -r以获得正确的源并进行构建。 You might need to do this by hand since the output of uname -r isn't always helpful.您可能需要手动执行此操作,因为uname -r的 output 并不总是有帮助。

You'll also need to secify the architecture and cross compilation toolchain prefix in the module Makefile , for example:您还需要在模块Makefile中指定架构和交叉编译工具链前缀,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM