2
votes

I have to write a simple linux kernel module for a study research project, but I am having trouble with make. This is what my Makefile looks like right now:

obj-m := main.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

The source file I want to compile is named main.c, and when I type make in the source code directory I get this output:

root@debian:~/test-module# make
make -C /lib/modules/4.9.0-4-amd64/build M=/root/test-module modules
make[1]: Entering directory '/lib/modules/4.9.0-4-amd64/build'
make[1]: *** No rule to make target 'modules'.  Stop.
make[1]: Leaving directory '/lib/modules/4.9.0-4-amd64/build'
Makefile:6: recipe for target 'all' failed
make: *** [all] Error 2

As the output already suggests, I am running Debian 9 with kernel 4.9.0-4-amd64. Since I am pretty new to Makefiles, I can't find any errors in the file. Could somebody please explain to me where my error is?

UPDATE: After some research I found out that /lib/modules/4.9.0-4-amd64/build must contain the kernel source tree. So i did

ln -s /usr/src/linux-source-4.9 /lib/modules/4.9.0-4-amd64/build

where the link's target directory contains the complete linux kernel source tree. When I run make now, I get this output:

root@debian:~/test-module# make
make -C /lib/modules/4.9.0-4-amd64/build modules
make[1]: Entering directory '/usr/src/linux-source-4.9'
scripts/kconfig/conf  --silentoldconfig Kconfig
***
*** Configuration file ".config" not found!
***
*** Please run some configurator (e.g. "make oldconfig" or
*** "make menuconfig" or "make xconfig").
***
scripts/kconfig/Makefile:37: recipe for target 'silentoldconfig' failed
make[3]: *** [silentoldconfig] Error 1
Makefile:548: recipe for target 'silentoldconfig' failed
make[2]: *** [silentoldconfig] Error 2

The present kernel configuration has modules disabled.
Type 'make config' and enable loadable module support.
Then build a kernel with module support enabled.

Makefile:1271: recipe for target 'modules' failed
make[1]: *** [modules] Error 1
make[1]: Leaving directory '/usr/src/linux-source-4.9'
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 2

Looks better than last time, but still doesn't work. I guess I just have to run a make config or so in some directory of the kernel source tree, but I don't know where. What should I do?

1
make -C /lib/modules/$(shell uname -r)/build means goto /lib/modules/4.9.0-4-amd64/build and run the makefile there. I think you should post that makefile.lockcmpxchg8b
Well, it also goes without saying that the Makefile in this question also doesn't specify a target called modules.tripleee
Did you remember to install the kernel development package?Ignacio Vazquez-Abrams
@lockcmpxchg8b The directory /lib/modules/4.9.0-4-amd64/build did not exist, so I decided to just create it. I followed this tutorial for my makefile.Sandtler
@IgnacioVazquez-Abrams I have the following packages installed: build-essential linux-source libncurses5-devSandtler

1 Answers

1
votes

I finally found out the error: I forgot to specify the M variable in my makefile. It works properly now. This is what my makefile looks like right now, if future users want to know:

obj-m := testmodule.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Thank you all for your help anyways!