【问题标题】:init.h file not found未找到 init.h 文件
【发布时间】:2022-01-06 23:58:12
【问题描述】:

我试图从 Linux Device Drivers 一书中做一个 hello world 模块。 但我从 gcc 收到了一个致命错误,

fatal error: linux/init.h: No such file or directory
    1 | #include<linux/init.h>

我已经安装了 linux 头文件

sudo apt install linux-headers-$(uname -r)

上面写着:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
linux-headers-5.10.0-9-amd64 is already the newest version (5.10.70-1).
The following package was automatically installed and is no longer required:
  libc-devtools
Use 'sudo apt autoremove' to remove it.
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.

我的代码:

#include<linux/init.h>

#include<linux/module.h>

static int hello_init(void)
{
    printk(KERN_ALERT,"Hello world\n");
}


static void hello_exit(void)
{
    printk(KERN_ALERT,"Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);
``

【问题讨论】:

标签: c linux kernel


【解决方案1】:

和你一样,我尝试了解 linux 设备驱动程序,并尝试了第一个示例(hello.c):

所有这一切都是因为我们不在一个合适的环境中,我认为我们应该使用另一个 linux 系统(一个“主线”系统,如果我没记错的话,就像在上一章中建议的那样,这很重要,如果你想要避免任何问题,或者更糟的是,使用本书中显示的示例使您的系统不稳定/丢失重要数据)。

但我终于让“hello world”工作了:

  1. 进入examples/misc-modules目录(“hello.c”所在的目录)
  2. 编辑 Makefile 并将所有其他模块放在注释中,因此您应该只看到“obj-m := hello.o”(将所有其他模块放在下面的行中并以“#”开头)
  3. 执行命令“make”,你应该有你的“hello.ko”

要查看模块 hello.ko 的消息,请在另一个终端中运行 dmesg -w(这些是内核消息,它们不会在标准控制台上看到!)

rem : "hellop.ko" 也可以构建,但它会因 seq.c 失败(另一个错误,函数的隐式定义,缺少标题或类似的东西......)

【讨论】:

    猜你喜欢
    • 2015-04-10
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 2021-09-17
    • 2016-09-22
    • 2019-05-06
    • 2023-04-01
    • 2014-04-17
    相关资源
    最近更新 更多