0

I am currently trying to learn how to make kernel modules from the 'Linux Device Drivers' book. I have the basic example typed out as follows:

#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("Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

and I am using the following makefile to compile the file above, hello.c

KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

When I run the make command, the console output is as follows:

make -C /lib/modules/5.13.0-40-generic/build M=/home/finlay/src/linux-drivers modules
make[1]: Entering directory '/usr/src/linux-headers-5.13.0-40-generic'
  MODPOST /home/finlay/src/linux-drivers/Module.symvers
make[1]: Leaving directory '/usr/src/linux-headers-5.13.0-40-generic'

For some reason the only files which are created are the following:

  • .Module.symvers.cmd
  • .modules.order.cmd
  • Module.symvers
  • modules.order

None of the files created seems to specifically be about the hello.c file. What's very strange is that this same file was working initially to create the hello.ko file however it is no longer working. Any ideas as to why this is not working?

finlay morrison
  • 225
  • 1
  • 5
  • What tells it to make a module hello.ko based on hello.c? – user253751 Apr 27 '22 at 10:42
  • The makefile for doing that is in the `/usr/src/linux-headers-5.13.0-40-generic/build` directory, I am calling that makefile from my own makefile. As I said, it was working to make the .ko file initially, however, it is no longer working. – finlay morrison Apr 27 '22 at 10:43

1 Answers1

0

I have now found the solution, I removed some lines from the makefile as I believed them to be redundant as I thought that they were only used if you were directly invoking the makefile from the kernel build directory. The correct makefile is below.

ifneq ($(KERNELRELEASE),)

obj-m := hello.o

else

KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

endif
finlay morrison
  • 225
  • 1
  • 5