You are not logged in.

#1 2014-05-30 18:49:58

Registered: 2014-01-01
Posts: 129

Specifying in the makefile the path to kernel.


I've been reading a book called Linux Device Drivers 3rd Edition.  One of the recommendations that was suggested was using this makefile in order to reduce the amount of typing that needs to be done (on page 24):

# if KERNELRELEASE is defined, we've been invoked from the kernel build system
#   and can use its language.
ifneq ($(KERNELRELASE),)
        obj-m := hello.o

# otherwise we were called directly from the command line; invoke the kernel
#   build system.

        KERNELDIR ?= ~/Downloads/linux_kernel/linux-
        PWD := $(shell pwd)

# this code will run on the 2nd run of the Makefile.
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

I did that.  Here's the driver code (very simple):

// hello.c:

#include <linux/init.h>
#include <linux/module.h>


static int hello_init(void)
  printk(KERN_ALERT " Hello, world!\n");

  return 0;

static void hello_exit(void)
  printk(KERN_ALERT " Goodbye, cruel world!\n");


Now, when I compile, this is what I get:

$ make && ll
make -C ~/Downloads/linux_kernel/linux- M=~/Documents/development/drivers/code_005 modules
make[1]: Entering directory `~/Downloads/linux_kernel/linux-'
  Building modules, stage 2.
  MODPOST 0 modules
make[1]: Leaving directory `~/Downloads/linux_kernel/linux-'
total 8
-rw-r--r--. 1 publicus staff 318 May 29 13:23 hello.c
-rw-r--r--. 1 publicus staff 442 May 30 14:28 Makefile
-rw-r--r--. 1 publicus staff   0 May 30 14:48 modules.order
-rw-r--r--. 1 publicus staff   0 May 30 14:41 Module.symvers

Now, when my Makefile has just obj-m := hello.o in it and I compile while explicitly specifying the module, the compilation works and I get my hello.ko file.  Why is that?


Board footer

Powered by FluxBB