Sineatos
Sineatos

Reputation: 1017

Writing a makefile for a kernel module

I want to write a Kernel Module and now I have written some files: a.c, b.c, b.h and d.h.

a.c includes b.h and d.h and b.h includes d.h too.

I wrote a Makefile like this:

ifneq ($(KERNELRELEASE),)
mymodule-objs :=a.c b.c
obj-m += a.o b.o
else
PWD := $(shell pwd)
KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
    rm -rf *.o *.mod.c *.symvers *order *.markers *.cmd *-
    $(MAKE) -C $(KDIR) M=$(PWD)
clean:
    rm -rf *.o *.mod.c *.symvers *order *.markers *.cmd *-
endif

But it doesn't work, how should I write a correct Makefile? I want to get a file name x.ko in end and.

After I use the 'make' command, and I use 'insmod' is give me a message:

insmod: ERROR: could not insert module a.ko: Unknown symbol in module

By the way I use Ubuntu 14.10. The kernel is 3.16.0-37-generic

Upvotes: 2

Views: 1122

Answers (2)

talshorer
talshorer

Reputation: 692

obj-m += a.o b.o

will create two modules, a.ko and b.ko
if you want to create a single module out of both (which I suppose you do because of the line with mymodule-objs), replace that line with

obj-m += mymodule.o

mymodule.o will be built according to mymodule-objs and then turned into mymodule.ko using modpost.

And as said before, you're missing modules in your $(MAKE) line

Upvotes: 1

ackerleytng
ackerleytng

Reputation: 416

Try modelling your Makefile after this?

https://unix.stackexchange.com/questions/122095/understanding-a-make-file-for-making-ko-files

You might not need mymodule-objs := a.c b.c. And I think you're missing modules after the $(MAKE) line

Upvotes: 0

Related Questions