compiling modules

5 views
Skip to first unread message

kavitha...@gmail.com

unread,
Apr 9, 2009, 1:40:50 AM4/9/09
to linuxgurus
hai

I worte a simple hello world module. I'm trying to compile the module
using gcc

I tried with this command
"gcc -W -o2 -D__KERNEL__ -DMODULE -I /lib/modules/`uname -r`/build -c
-o hello.o hello.c "
when i do this i get a error saying <linux/module.h> no such file or
directory

so, i commented that line ,now i got the hello.o file
I tried linking with
"ld -m elf_i386 -m elf_i386 -r -o /root/one/hello/hello.ko /root/one/
hello/hello.o"
But when i tried inserting the module It says : invalid module format

Now, How can i compile the module without make or makefile?

Regards
Kavitha.C.K

mahesh war

unread,
Apr 9, 2009, 9:43:39 AM4/9/09
to linux...@googlegroups.com
Hello This is Maheshwar.

i am writing simple module for 2.4 kernel

module name is hello.c: vi hello.c

#define __KERNEL__
#define MODULES
#include<linux/init.h>
#include<linux/module.h>
#include<linux/kernel.h>

MODULE_LICENSE("GPL");

static int hello_init(void)
{
printk("\nhello maheshwar");
}

static void hello_exit(void)
{
printk("\nbye maheshwar");
return 0;
}
module_init(hello_init);
module_exit(hello_exit);

compile it using the following command:
gcc -I /usr/src/linux-2.4/include -Wall -c hello.c

you will get hello.o file ,insert the module into the kernel using following command
insmod hello.o
run dmesg
remove the module from the kernel using following command:
rmmod hello


for 2.6 kernel run following 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

tldp.org/LDP/lkmpg/2.6/html/lkmpg.html --ENJAY

Jagadeesh Dyaberi

unread,
Apr 9, 2009, 1:27:29 PM4/9/09
to linux...@googlegroups.com
you will still run into the error: <linux/module.h> no  such file or directory with the above Makefile.

First check your linux-headers under and ensure the file exists:
/usr/src/your_kernel/include/linux/module.h

If it doesnt, then you may need to compile your kernel and modules

kavitha...@gmail.com

unread,
Apr 11, 2009, 8:02:10 AM4/11/09
to linuxgurus
hai Maheshwar
Thank You for the reply.
But I'm doing it on 2.6.18 kernel and I want to compile my module
without using Makefile.

Regards
Kavitha.C.K

Lokesh

unread,
May 26, 2009, 11:22:07 AM5/26/09
to linuxgurus
I dont think there is a need of compiling the module without a
Makefile.
As the module compilation follows a procedure called kbuild from 2.6.x
kernel
So, from wherever you compile the module which has a capability to add
into the kernel
directly. I find compiling by Makefile has too many options.

If you still want to look into it then yes we can do this.
Compile the kernel with Makefile first and then read all the files
created during the compilation.

1. Compiles with kernel source, generates .o (object) file
2. Uses ld command link to .ko file

Have a happy hacking
Reply all
Reply to author
Forward
0 new messages