title: Linux device driver module compilation using gcc
body: <p>I have a basic linux device driver module :</p>
  
  <pre><code>#include &lt;linux/init.h&gt;
  #include &lt;linux/module.h&gt;   
  #include &lt;linux/kernel.h&gt;
  
  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);
  </code></pre>
  
  <p>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).</p>
  
  <p>So is there a way to compile using command line gcc ??</p>
  
  <p><strong>EDIT</strong> Attaching the Makefile I have used</p>
  
  <pre><code>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
  </code></pre>