4

I have been trying for hours to create my first Hello World Kernel module, alas unsuccessfully. My simple C code (hello.c) and makefile are located in /Downloads on my system, if that should be of any importance.

hello.c

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

module_init(hello_init);
module_exit(hello_exit);

makefile

obj−m += hello.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

And this is the error message I'm receiving when I try to build the kernel module by entering "make" (without quotation marks) in the terminal:

make -C /lib/modules/4.2.0-16-generic/build
M=/home/username/workspace/test/Module modules make[1]:
Entering directory '/usr/src/linux-headers-4.2.0-16-generic'
scripts/Makefile.build:44:
/home/username/workspace/test/Module/Makefile: No such file or
directory make[2]: *** No rule to make target
'/home/username/workspace/test/Module/Makefile'.  Stop.
Makefile:1398: recipe for target
'_module_/home/username/workspace/test/Module' failed make[1]:
*** [_module_/home/username/workspace/test/Module] Error 2 make[1]: Leaving directory '/usr/src/linux-headers-4.2.0-16-generic'
makefile:4: recipe for target 'all' failed make: *** [all] Error 2

I have tried pretty much every single solution and have followed every suggestion regarding this problem that can be found on google. Nothing could solve my problem ... If anybody knows anything I could try, I would be very grateful!

  • Should I move my hello.c and makefile to /usr/src?
  • Are there any syntax errors in my files?
  • Should it say hello.c instead of hello.o in the makefile?
  • Might my Ubuntu be missing something?
  • ...
Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
ci7i2en4
  • 834
  • 1
  • 13
  • 27
  • Read "Linux Device Drivers 3rd Edition." Great book, also describing the building process in particular. 4th edition coming soon! – cadaniluk Oct 28 '15 at 18:28
  • I have read the first few sections but obtaining a mainline kernel (I tried 4.3) and installing it alongside the existing kernel, as the instructions suggest, didn't solve my problem either. After all the research I've done today I believe the error is somehow related to the makefile ... – ci7i2en4 Oct 28 '15 at 18:37
  • Runs fine for me except no `.ko` file being produced. – cadaniluk Oct 28 '15 at 18:40
  • It is supposed to run fine as I copied the codes from the book you mentioned. If it's working for you, something on your system must be different ... – ci7i2en4 Oct 28 '15 at 18:42
  • Is "username" really your username? Or did you substitute the real one for the sake of information (un-)disclosure? – cadaniluk Oct 28 '15 at 18:46
  • For the sake of trying I installed the mainline kernel 4.3rc7 again and now "make" gives me the following errors: make -C /lib/modules/4.3.0-040300rc7-generic/build M=/home/username/workspace/test/Module modules make[1]: Entering directory '/usr/src/linux-headers-4.3.0-040300rc7-generic' make[1]: Makefile: No such file or directory make[1]: *** No rule to make target 'Makefile'. Stop. make[1]: Leaving directory '/usr/src/linux-headers-4.3.0-040300rc7-generic' makefile:4: recipe for target 'all' failed make: *** [all] Error 2 – ci7i2en4 Oct 28 '15 at 18:54

1 Answers1

1

I just changed the name "makefile" to "Makefile" (upper case M) and that seems to have done something. Now I'm getting:

make -C /lib/modules/4.2.0-16-generic/build M=/home/username/workspace/test/Module modules make[1]: Entering directory '/usr/src/linux-headers-4.2.0-16-generic' Building modules, stage 2. MODPOST 0 modules make[1]: Leaving directory '/usr/src/linux-headers-4.2.0-16-generic'

^^ I knew it from the beginning that somehow my makefile is causing this problem! Be that as it may, I still don't have a hello.ko file.

Any suggestions what I should try now?

OMG !!!

Googled this new problem and found this answer from dsf: Linux: modpost does not build anything (last posting)

Just as they suggest I typed the text in the Makefile manually and after that, as if by magic, it worked perfectly fine! Now I've got my hello.ko, but, unfortunately, no explanation. Maybe, if you just copy the Makefile text from a pdf/website, you do end up with unwanted characters, as dsf seems to believe?!

Anyway, problem solved.

Community
  • 1
  • 1
ci7i2en4
  • 834
  • 1
  • 13
  • 27