我正在尝试实现 Silberschatz 的《操作系统概念》书中的示例。该示例是关于添加和删除内核模块的。到目前为止,它已经给出了一个 simple.c 文件和一个 Makefile,该文件相应地打印有关向内核添加和删除模块的消息。我的问题从一开始就开始,找不到 linux/init.h 。在
sudo apt install linux-header-generic
和 sudo apt-get install linux-source
收到此错误后,我已经安装了必要的软件包,但错误仍然存在。
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
}
我添加这个文件是因为在某些主题中,有一些修改建议,但我不明白该怎么做,我想添加它。 还有其他一些主题建议对 Makefile 进行一些修改,但我相信这是关于库的,而不是 Makefile,因为我在编译之前在 VS Code 上收到此错误。
simple.c 和 makefile,以及 gcc 和 make 的输出如下:
#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");
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.
首先我安装了相关依赖,但是没有用。然后,我尝试根据此链接修改Makefile,但它给出了错误
Makefile:9: *** missing separator. Stop.
。
在#include 宏中:
- For standard library header like stdio.h use <> (<stdio.h>).
- For other library headers like linux/init.h use double-quotes ""
("linux/init.h").
这应该可以解决 vscode 警告。
关于 gcc 错误,您正在使用 c17 标准编译内核,但我读到了一些 linux 内核是使用 GNU 特定的 c89 标准
编译的内容(gcc -std=gnu89)。