this is my first time trying to create a kernel module, apparently after creating a module I need to follow these steps to compile it:

echo "obj-m := Hello.c" > Makefile
make -C /lib/modules/`uname -r`/build M=$PWD modules

here's my problem, there is no "build" in "/lib/modules/uname -r/" directory!

what am I missing here?

  • What flavor / distribution of Linux are you using? – steeldriver Apr 19 '17 at 19:49
  • @steeldriver Debian Jessie, sorry forgot to mention it. – Brian SP2 Apr 19 '17 at 20:03

just found it, the following package is missing and needs to be installed before compiling any Linux kernel module:

sudo apt-get install -y linux-headers-`uname -r`

then we're ready to compile the module

echo "obj-m := Hello.c" > Makefile
make -C /lib/modules/`uname -r`/build M=$PWD modules
sudo insmod Hello.ko
  • 1
    I'm more familiar with Ubuntu, but I believe it's recommended to install the appropriate architecture-specific metapackage instead - that way, the headers should be updated automatically when the kernel is updated e.g. linux-headers-amd64 – steeldriver Apr 19 '17 at 23:16
  • @steeldriver I believe this is right and more appropriate, you should add this as an answer, I'll set it as main solution, and tnx – Brian SP2 Apr 20 '17 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.