没有内核Makefile的Linux内核模块如何编译? [英] How to compile Linux kernel module without kernel Makefile?

查看:539
本文介绍了没有内核Makefile的Linux内核模块如何编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ARM Linux设备,但没有内核的Makefile来构建内核模块.

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

我有GCC交叉编译器到这个拱门.

I have GCC cross compiler to this arch.

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

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

推荐答案

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

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

不能.您需要内核Makefile来编译模块,以及预构建的内核源代码树.在大多数发行版中,您可以通过linux-headers-xxx之类的包获得用于构建模块的内核源,其中xxx应该是uname -r的输出.

You can't. You need the kernel Makefile in order to compile a module, along with a pre-built kernel source tree. 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.

例如,在Debian或Ubuntu上:

For example, on Debian or Ubuntu:

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

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

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:

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告诉内核在当前目录中构建模块.

What this does is basically invoking the kernel Makefile telling it to build the module in the current directory.

此外,我不确定为什么要说您在ARM Linux设备上,并且您有交叉编译器.如果您在设备本身上,则完全不需要交叉编译器.如果您使用的是其他设备,则需要为目标设备使用适当的uname -r,以获取正确的源代码并进行构建.您可能需要手动执行此操作,因为uname -r的输出并不总是有用的.

Also, I am not sure why you say that you are on an ARM Linux device, and you have a cross compiler. 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. You might need to do this by hand since the output of uname -r isn't always helpful.

您还需要保护模块Makefile中的体系结构和交叉编译工具链前缀,例如:

You'll also need to secify the architecture and cross compilation toolchain prefix in the module Makefile, for example:

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

这篇关于没有内核Makefile的Linux内核模块如何编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆