linux - make 拒绝构建内核模块

标签 linux makefile module compilation kernel

我必须为一个研究项目编写一个简单的 linux 内核模块,但是我在使用 make 时遇到了问题。这就是我的 Makefile 现在的样子:

obj-m := main.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

我要编译的源文件名为 main.c,当我在源代码目录中键入 make 时,我得到以下输出:

root@debian:~/test-module# make
make -C /lib/modules/4.9.0-4-amd64/build M=/root/test-module modules
make[1]: Entering directory '/lib/modules/4.9.0-4-amd64/build'
make[1]: *** No rule to make target 'modules'.  Stop.
make[1]: Leaving directory '/lib/modules/4.9.0-4-amd64/build'
Makefile:6: recipe for target 'all' failed
make: *** [all] Error 2

如输出所示,我正在运行内核为 4.9.0-4-amd64 的 Debian 9。因为我对 Makefiles 很陌生,所以我在文件中找不到任何错误。有人可以向我解释我的错误在哪里吗?

更新: 经过一些研究,我发现 /lib/modules/4.9.0-4-amd64/build 必须包含内核源代码树。所以我做了

ln -s /usr/src/linux-source-4.9 /lib/modules/4.9.0-4-amd64/build

链接的目标目录包含完整的 linux 内核源代码树。当我现在运行 make 时,我得到这个输出:

root@debian:~/test-module# make
make -C /lib/modules/4.9.0-4-amd64/build modules
make[1]: Entering directory '/usr/src/linux-source-4.9'
scripts/kconfig/conf  --silentoldconfig Kconfig
***
*** Configuration file ".config" not found!
***
*** Please run some configurator (e.g. "make oldconfig" or
*** "make menuconfig" or "make xconfig").
***
scripts/kconfig/Makefile:37: recipe for target 'silentoldconfig' failed
make[3]: *** [silentoldconfig] Error 1
Makefile:548: recipe for target 'silentoldconfig' failed
make[2]: *** [silentoldconfig] Error 2

The present kernel configuration has modules disabled.
Type 'make config' and enable loadable module support.
Then build a kernel with module support enabled.

Makefile:1271: recipe for target 'modules' failed
make[1]: *** [modules] Error 1
make[1]: Leaving directory '/usr/src/linux-source-4.9'
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 2

看起来比上次好多了,但还是不行。我想我只需要在内核源代码树的某个目录中运行 make config 左右,但我不知道在哪里。我该怎么办?

最佳答案

我终于发现了错误:我忘记在我的 makefile 中指定 M 变量。它现在工作正常。这就是我的 makefile 现在的样子,如果 future 的用户想知道的话:

obj-m := testmodule.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

无论如何,谢谢大家的帮助!

关于linux - make 拒绝构建内核模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47520636/

相关文章:

c++ - 为 Windows x64 构建 log4cplus

ruby - ruby eigen 类中的模块包含

linux - 如何将 PKCS#12 导入 Seahorse

c# - 如何在 C# 中运行一系列进程并保留其环境设置?

linux - Python3 mlpy 安装错误 - ‘Py_InitModule3’ 未在此范围内声明

installation - 使用一些额外的 CFLAGS 安装 Glibc

python - 如何在python中控制系统上是否安装了库

ruby - 如何在 ruby​​ 模块之间共享方法

c - 当我在终端中按 Ctrl+C 时,执行 C 中的最后一个函数

c - memset 无法正常工作