使用 gcc 编译 Linux 设备驱动模块

标签 linux gcc linux-device-driver

我有一个基本的 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/

相关文章:

python - 安装 line_profiler 后如何修复 pip 被 proken?

linux - Ansible "missing become password"在一台机器上但不是另一台机器

linux - 使用 awk 从文件中提取信息并使用 bash 将其存储到变量中

c++ - GCC -fstack-check 选项在 C 中引发了什么异常

linux - 我们如何从一个小型存储笔式驱动器中使用 Linux?它也适用于微 Controller 吗?

linux - 使用Qt5远程调试程序时QXcbConnection : Could not connect to display,

c++ - 写入 [ds :0x0]? 后添加 ud2 有什么意义

gcc - 防止递归 C#include

linux - 读/写字符设备时如何避免高CPU使用率?

linux - 如何使用 devm_regulator_get 处理错误