1

I am new to kernel programming. I ahve written a hello world program but I am not able to complie it. I have serached and so made a make file under /usr/src and then doing sudo make command to run it. But its giving the following error:

make -C /lib/modules/3.2.0-23-generic-pae/build M=/usr/src modules
make[1]: Entering directory `/usr/src/linux-headers-3.2.0-23-generic-pae'
make[2]: *** No rule to make target `/usr/src/hello.c', needed by  `/usr/src   /hello.o'.  Stop.
make[1]: *** [_module_/usr/src] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.2.0-23-generic-pae'
make: *** [all] Error 2

make file:

obj-m += hello.o

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

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

hello.c (in ~Desktop/inet/)

#include <linux/module.h>   
#include <linux/kernel.h>   
int init_module(void)
{
   printk(KERN_INFO "Hello world 1.\n");

 /* 
 * A non 0 return means init_module failed; module can't be loaded. 
 */
  return 0;
}

void cleanup_module(void)
{
  printk(KERN_INFO "Goodbye world 1.\n");
}

In makefile I have taken care that every new line has a tab space only!

Please can some tell the problem!

4
  • Too bad formatting. Please format the code and output messages correctly.
    – ForceBru
    Mar 6, 2015 at 13:49
  • 1
    Are you running make from /usr/src? And your source file lives in $HOME/Desktop/inet? The error is from make not knowing how to build a file that doesn't exist (/usr/src/hello.c) because that's not where your file is. I don't know the kernel build system enough to know what the right fix is here but that's the problem. Mar 6, 2015 at 13:54
  • your Makefile should be ~Desktop/inet/ and not in /usr/src Mar 6, 2015 at 13:59
  • if my memory is correct, you need to claim init_module as __attribute__((constructor)) . similar to cleanup_module as destructor.
    – Jason Hu
    Mar 6, 2015 at 14:08

2 Answers 2

0

Move your Makefile to where the module source hello.c is. That means in ~/Desktop/inet/. Looks like you have put the Makefile in /usr/src/

$ sudo mv /usr/src/Makefile ~/Desktop/inet/
$ cd ~/Desktop/inet/
$ make
0
0

just use make instead of sudo make

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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