【问题标题】:Why is my hello-1.c and Makefile not producing .ko files?为什么我的 hello-1.c 和 Makefile 没有生成 .ko 文件?
【发布时间】:2021-04-28 20:10:37
【问题描述】:

我正在关注 Linux 内核模块编程指南的 hello world 编程示例(第 8-9 页):https://tldp.org/LDP/lkmpg/2.6/lkmpg.pdf

如指南中所述,我有以下 hello-1.c:

/*
 * hello−1.c − The simplest kernel module.
 */
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */

int init_module(void) {
    printk(KERN_INFO "Hello world 1.\n");
    /*
    * A non 0 return means init_module failed; module can't be loaded.
    */
    return 0;
}

void cleanup_module(void) {
    printk(KERN_INFO "Goodbye world 1.\n");
}

连同第 9 页的 Makefile:

obj−m += hello−1.o

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

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

当我将 Makefile 与 make 一起使用时,我得到以下输出:

make -C /lib/modules/5.4.0-60-generic/build M=/home/joeyoneill/Desktop/CSCI614 modules
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-60-generic'
  Building modules, stage 2.
  MODPOST 0 modules
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-60-generic'

制作了 2 个文件(Module.symvers 和 modules.order)。

但是,我没有得到:

hostname:~/lkmpg−examples/02−HelloWorld# make
make −C /lib/modules/2.6.11/build M=/root/lkmpg−examples/02−HelloWorld modules
make[1]: Entering directory `/usr/src/linux−2.6.11'
 CC [M] /root/lkmpg−examples/02−HelloWorld/hello−1.o
 Building modules, stage 2.
 MODPOST
 CC /root/lkmpg−examples/02−HelloWorld/hello−1.mod.o
 LD [M] /root/lkmpg−examples/02−HelloWorld/hello−1.ko
make[1]: Leaving directory `/usr/src/linux−2.6.11'
hostname:~/lkmpg−examples/02−HelloWorld#

这本书给出的输出是什么,所以我没有创建这个 .o 或 .ko 文件。 用线条显示:

 CC /root/lkmpg−examples/02−HelloWorld/hello−1.mod.o
 LD [M] /root/lkmpg−examples/02−HelloWorld/hello−1.ko

谁能告诉我我做错了什么?

【问题讨论】:

  • 这种情况的常见原因是从 PDF 文件复制。如果这是您所做的,那么建议您启动一个新的 makefile 并输入内容。
  • 或者找个文本编辑器显示异常字符。
  • 在你的 Makefile obj-m 使用了错误的破折号。

标签: c linux ubuntu makefile linux-kernel


【解决方案1】:

现在为 v5.x 内核维护了 Linux 内核模块编程指南,您可以获取本书以及工作示例:https://github.com/sysprog21/lkmpg

【讨论】:

    猜你喜欢
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    相关资源
    最近更新 更多