2

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?

Sandtler
  • 43
  • 2
  • 8
  • `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 Nov 28 '17 at 02:13
  • Well, it also goes without saying that the `Makefile` in this question *also* doesn't specify a target called `modules`. – tripleee Nov 28 '17 at 06:22
  • Did you remember to install the kernel development package? – Ignacio Vazquez-Abrams Nov 28 '17 at 06:25
  • @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](http://tldp.org/LDP/lkmpg/2.6/html/x181.html) for my makefile. – Sandtler Nov 28 '17 at 23:57
  • @IgnacioVazquez-Abrams I have the following packages installed: `build-essential linux-source libncurses5-dev` – Sandtler Nov 29 '17 at 00:04
  • So...kernel makefiles aren't supposed to stand alone, apparently. See https://stackoverflow.com/questions/29231876/how-does-kbuild-actually-work. That makefile is supposed to be called by a makefile higher up the makefile hierarchy; one that knows what to do with the `obj-m` variable that is being set in the local makefile. – lockcmpxchg8b Nov 29 '17 at 01:53
  • I see. So what I did now is delete the build folder I created and `ln -s /usr/src/linux-source-4.9 /lib/modules/4.9.0-4-amd64/build`. This fixed the issue with the missing target for `modules`, but created another error. I will update my question. – Sandtler Nov 29 '17 at 12:47

1 Answers1

1

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!

Sandtler
  • 43
  • 2
  • 8