LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Compile a kernel module (https://www.linuxquestions.org/questions/linux-newbie-8/compile-a-kernel-module-423608/)

Ralc 03-10-2006 03:05 PM

Compile a kernel module
 
I'm having problems to compile a simple Hello World module. I'm using the kernel 2.6 with a SUSE 10.0 distribution and gcc 4.0.2.
The problem is that the Makefile doesn't compile the module, it doesnt create .o or .ko objects. Can anyone help?

*** Here is the code:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

MODULE_LICENSE("GPL");

static int hello_init(void)
{
printk(KERN_ALERT "Hello World\n");
return 0;
}
static void hello_cleanup(void) {
printk(KERN_ALERT "Bye!\n");
}
module_init(hello_init);
module_exit(hello_cleanup);

*** The Makefile:

obj-m := hello.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

*** The Output:

unit1:/home/rui/module # make
make -C /lib/modules/2.6.13-15-smp/build SUBDIRS=/home/rui/module modules
make[1]: Entering directory `/usr/src/linux-2.6.13-15-obj/i386/smp'
make -C ../../../linux-2.6.13-15 O=../linux-2.6.13-15-obj/i386/smp modules
Building modules, stage 2.
MODPOST
make[1]: Leaving directory `/usr/src/linux-2.6.13-15-obj/i386/smp'

jcliburn 03-10-2006 09:39 PM

You need a tab character before $(MAKE) in the default rule. Not spaces... a tab.

Ralc 03-11-2006 07:35 AM

Actually there is a TAB before $(MAKE) in my Makefile. Without the tab the message is diferent. It says that there is nothing to be done for default.
The question continues...

jcliburn 03-11-2006 08:06 AM

Sorry, looks like no whitespace at all in the original post.

I have to rebuild a kernel network module with each new Fedora kernel release if I want IPv6 to work. Here's how I do it. Maybe it'll help...

Code:

[root@osprey net]# pwd
/usr/src/redhat/BUILD/kernel-2.6.15/linux-2.6.15/drivers/net
[root@osprey net]# ls -l via-velocity*
-rw-r--r--  2 root root 88117 Mar  2 18:28 via-velocity.c
-rw-r--r--  2 root root 50028 Jan  2 21:21 via-velocity.h
[root@osprey net]# cat Makefile
obj-m := via-velocity.o

KDIR  := /lib/modules/$(shell uname -r)/build
PWD  := $(shell pwd)

default:
        $(MAKE) -C $(KDIR) M=$(PWD) modules

[root@osprey net]# make
make -C /lib/modules/2.6.15-1.1833_FC4/build M=/usr/src/redhat/BUILD/kernel-2.6.15/linux-2.6.15/drivers/net modules
make[1]: Entering directory `/usr/src/kernels/2.6.15-1.1833_FC4-x86_64'
  CC [M]  /usr/src/redhat/BUILD/kernel-2.6.15/linux-2.6.15/drivers/net/via-velocity.o
  Building modules, stage 2.
  MODPOST
  CC      /usr/src/redhat/BUILD/kernel-2.6.15/linux-2.6.15/drivers/net/via-velocity.mod.o
  LD [M]  /usr/src/redhat/BUILD/kernel-2.6.15/linux-2.6.15/drivers/net/via-velocity.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.15-1.1833_FC4-x86_64'
[root@osprey net]# ls -l via-velocity*
-rw-r--r--  2 root root  88117 Mar  2 18:28 via-velocity.c
-rw-r--r--  2 root root  50028 Jan  2 21:21 via-velocity.h
-rw-r--r--  1 root root 426144 Mar 11 07:58 via-velocity.ko
-rw-r--r--  1 root root  2957 Mar 11 07:58 via-velocity.mod.c
-rw-r--r--  1 root root  44960 Mar 11 07:58 via-velocity.mod.o
-rw-r--r--  1 root root 382592 Mar 11 07:58 via-velocity.o
[root@osprey net]#


Ralc 03-11-2006 08:43 AM

That's exactly what I wanted. But didn't get it...
I'm wondering about the file vermagic.o. That file should be in /init and doesn't appear in my directory. I read that all the modules must pass through a linking step with this file. Does it appear for you? Do you know how can I create it? Maybe I forgot something in the kernel's configuration.

jcliburn 03-11-2006 08:51 AM

I have no vermagic.o anywhere on my system.

I did notice one difference between your Makefile and mine; you have "SUBDIRS=" where I have "M=". I don't really know if it's important or not.


All times are GMT -5. The time now is 04:35 PM.