You are not logged in.

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

publicus
Member
Registered: 2014-01-01
Posts: 129

Specifying in the makefile the path to kernel.

Hello,

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.
else

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

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

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

// hello.c:

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

MODULE_LICENSE("Dual BSD/GPL");

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");
}

module_init(hello_init);
module_exit(hello_exit);

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

$ make && ll
make -C ~/Downloads/linux_kernel/linux-2.6.32.62 M=~/Documents/development/drivers/code_005 modules
make[1]: Entering directory `~/Downloads/linux_kernel/linux-2.6.32.62'
  Building modules, stage 2.
  MODPOST 0 modules
make[1]: Leaving directory `~/Downloads/linux_kernel/linux-2.6.32.62'
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?

Offline

Board footer

Powered by FluxBB