更新时间:2021-09-09 03:18:25
在没有内核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