0
votes

I have a basic linux device driver module :

#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);

I am able to compile this module in traditional way which is by using a simple Makefile which uses obj-m , but I want to compile this using command line gcc. This is because I can use gcc -save-temps flag to see the intermediate generated files(this can be particularly helpful to understand as Linux kernel uses lot of preprocessor stuff).

So is there a way to compile using command line gcc ??

EDIT Attaching the Makefile I have used

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
2
Using gcc from the command line successfully depends on setting up the shell environment properly. That is (partly) what make and Makefile are for. If you really insist on using the shell, then you should append your Makefile to your question for review. - sawdust

2 Answers

1
votes

Could you try to add "EXTRA_CFLAGS" in your module's Makefile? such as EXTRA_CFLAGS += -save-temps

Hope it help you!

0
votes

I don't know how to to that directly in Makefile, but you can generate your .i file by file. From the root directory of the Linux kernel source:

make drivers/media/pci/sta2x11/sta2x11_vip.i

This will generate the .i file. To generate your module source file (which is outside the kernel tree) just use the absolute path to it:

make /path/to/hello.i

It should work