You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编译Linux内核模块时linux/init.h找不到及多工具报错求助

内核模块编译问题排查与解决

问题背景

正在实现Silberschatz所著《操作系统概念》中的内核模块增删示例,通过simple.c和Makefile实现模块加载/卸载时的内核日志打印,但遇到以下问题:

  • 编译前VS Code提示linux/init.h找不到,已安装linux-header-generic和linux-source但无效
  • 执行make命令报错
  • 修改Makefile后曾出现missing separator错误

现有文件内容

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "c++98",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

simple.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

/* This function is called when the module is loaded. */
int simple_init(void)
{
       printk(KERN_INFO "Loading Module\n");

       return 0;
}

/* This function is called when the module is removed. */
void simple_exit(void) {
    printk(KERN_INFO "Removing Module\n");
}

/* Macros for registering module entry and exit points. */
module_init( simple_init );
module_exit( simple_exit );

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");

Makefile

obj-m += simple.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

错误输出

make命令执行结果

make -C /lib/modules/6.5.0-35-generic/build M=/home/anil/Desktop/Operating Systems Concept modules
make[1]: Entering directory '/usr/src/linux-headers-6.5.0-35-generic'
make[2]: *** No rule to make target 'Systems'.  Stop.
make[1]: *** [Makefile:234: __sub-make] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-6.5.0-35-generic'
make: *** [Makefile:3: all] Error 2

gcc命令执行结果

12 | #include <linux/init.h>
      |          ^~~~~~~~~~~~~~
compilation terminated.

解决方案

1. 修复路径空格问题

从make输出可见,项目目录路径/home/anil/Desktop/Operating Systems Concept包含空格,导致make将Operating和Systems解析为两个独立目标。解决方式二选一:

  • 重命名项目目录,移除空格(推荐),比如改为OperatingSystemsConcept
  • 修改Makefile中M=$(PWD)为M="$(PWD)",用引号包裹路径:
obj-m += simple.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

2. 修正VS Code IntelliSense配置

VS Code提示头文件找不到是因为c_cpp_properties.json未包含内核头文件路径,更新配置如下:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/src/linux-headers-${command:system.unameRelease}/include/**",
                "/usr/src/linux-headers-${command:system.unameRelease}/arch/x86/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "c++98",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

添加内核头文件的路径后,IntelliSense就能正确识别linux/init.h等内核头文件,消除编译前的错误提示。

3. 确保Makefile格式正确

之前出现的missing separator错误是因为Makefile中命令行开头使用了空格而非Tab键。Makefile要求所有执行命令的行必须以Tab开头,替换所有命令行前的空格为Tab即可解决。

4. 验证内核头文件安装

执行以下命令确认当前内核版本对应的头文件已正确安装:

dpkg -l | grep linux-headers-$(uname -r)

如果没有输出,说明未安装对应版本的头文件,执行以下命令安装:

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

内容的提问来源于stack exchange,提问作者akoluaciklinux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 17:47:34