Linux 内核模块编程 - 无法使用 CONFIG_CC_STACKPROTECTOR_REGULAR: 编译器不支持 -fstack-protector

Linux kernel module programming - Cannot use CONFIG_CC_STACKPROTECTOR_REGULAR: -fstack-protector not supported by compiler

我在 Linux Ubuntu 14.04。我想开始 Linux 内核模块编程。我有 hello.c(简单的 Hello World 模块)和 Makefile。但是,在 "make" 命令上,我得到错误。

我尝试了 Cannot use CONFIG_CC_STACKPROTECTOR_STRONG: -fstack-protector-strong not supported by compiler,但它对我不起作用。

hello.c

/* hello.c − Illustrating the __init, __initdata and __exit macros. */

#include <linux/module.h>
/* Needed by all modules */
#include <linux/kernel.h>
/* Needed for KERN_INFO */
#include <linux/init.h>
/* Needed for the macros */

static int hello3_data __initdata = 3;

static int __init hello_3_init(void)
{
  printk(KERN_INFO "Hello, world %d\n", hello3_data);
  return 0;
}

static void __exit hello_3_exit(void)
{
  printk(KERN_INFO "Goodbye, world 3\n");
} 

module_init(hello_3_init);
module_exit(hello_3_exit);

生成文件

obj-m += hello.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" 上:-

k@k-Inspiron-3542:~/Kernel programs$ make
make -C /lib/modules/4.2.0-27-generic/build M=/home/k/Kernel programs modules
make[1]: Entering directory `/usr/src/linux-headers-4.2.0-27-generic'
arch/x86/Makefile:138: CONFIG_X86_X32 enabled but no binutils support
Makefile:662: Cannot use CONFIG_CC_STACKPROTECTOR_REGULAR: -fstack-protector not supported by compiler
make[1]: *** No rule to make target `programs'.  Stop.
make[1]: Leaving directory `/usr/src/linux-headers-4.2.0-27-generic'
make: *** [all] Error 2

目前,我的 ubuntu 有 4.2 内核。我什至在 3.x 内核上试过这个,但也有同样的错误。

请帮帮我。谢谢。 :)

您的文件在这里工作正常。使用您的 Makefile 和默认 Makefile:

obj-m   := hello.o

KDIR    := /lib/modules/$(shell uname -r)/build
PWD     := $(shell pwd)

default:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

clean:
    $(MAKE) -C $(KDIR) M=$(PWD) clean

文件 hello.ko, hello.mod.c, hello.mod.o, hello.o, modules.order, Module.symvers 已创建。可以安装这个:sudo apt install g++ binutils-dev

在问这个问题之前我已经搜索了很多,但没有一个解决方案适合我。我继续搜索,最后,这个解决方案对我有用。 https://askubuntu.com/questions/367838/compiling-error-while-installing-realtek-rtl8111e-in-64-bit-13-10-config-x86-x

奇怪的是,内核模块所在的目录名中应该没有space(s)。所以,我删除了 space 并且它起作用了。

希望它能对以后的人有所帮助。 :)

如果您的编译目录中没有空格并且仍然出现此错误,则您的内核编译可能会失败,因为它的目录属于 root 而您是 运行作为非特权用户。尝试 sudo make