我有一个基本的 linux 设备驱动程序模块:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
static int __init hello_init(void)
{
printk(KERN_ALERT "Hello, world \n");
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_ALERT "Goodbye, world \n");
}
module_init(hello_init);
module_exit(hello_exit);
我能够以传统方式编译此模块,即使用使用 obj-m 的简单 Makefile,但我想使用命令行 gcc 编译此模块。这是因为我可以使用 gcc -save-temps 标志来查看中间生成的文件(这对理解特别有帮助,因为 Linux 内核使用了很多预处理器的东西)。
那么有没有办法使用命令行gcc编译呢??
编辑附上我用过的Makefile
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
EXTRA_CFLAGS+= -save-temps
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
else
obj-m := hello.o
endif
最佳答案
您可以尝试在模块的 Makefile 中添加“EXTRA_CFLAGS”吗? 例如 EXTRA_CFLAGS += -save-temps
希望对你有帮助!
关于使用 gcc 编译 Linux 设备驱动模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15307951/