我目前正在阅读一本关于 Linux 内核模块编程的书 -https://tldp.org/LDP/lkmpg/2.4/lkmpg.pdf我在运行 make 时遇到编译错误。
/* hello.c − The simplest kernel module.
*
*/
/* Kernel Programming */
#define MODULE
#define LINUX
#define __KERNEL__
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_ALERT */
int init_module(void)
{
printk("<1>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_ALERT "Goodbye world 1.\n");
}
MODULE_LICENSE("GPL");
TARGET := hello
WARN := −W −Wall −Wstrict−prototypes −Wmissing−prototypes
INCLUDE := −isystem /lib/modules/`uname −r` /build/include
CFLAGS := −O2 −DMODULE −D__KERNEL__ ${WARN} ${INCLUDE}
CC := gcc
${TARGET}.o: ${TARGET}.c
.PHONY: clean
clean: rm −rf ${TARGET}.o
gcc −O2 −DMODULE −D__KERNEL__ −W −Wall −Wstrict−prototypes −Wmissing−prototypes −isystem /lib/modules/`uname −r` /build/include -c -o hello.o hello.c
uname: extra operand ‘−r’
Try 'uname --help' for more information.
gcc: error: −O2: No such file or directory
gcc: error: −DMODULE: No such file or directory
gcc: error: −D__KERNEL__: No such file or directory
gcc: error: −W: No such file or directory
gcc: error: −Wall: No such file or directory
gcc: error: −Wstrict−prototypes: No such file or directory
gcc: error: −Wmissing−prototypes: No such file or directory
gcc: error: −isystem: No such file or directory
gcc: error: /build/include: No such file or directory
make: *** [<builtin>: hello.o] Error 1
我收到的错误是 Makefile 中定义的命令选项。我不明白什么?
答案1
您需要使用连字符,-
而不是短破折号,−
。
您还需要删除多余的空间
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
(就在之前/build/include
)。