我必须为一个研究项目编写一个简单的 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/