compile hello world module...

Submitted by ni3_Linux
on August 14, 2006 - 7:02am

Hi friends i m getting problem while compiling 'hello world' module....

****** hello-1.c ********

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

int int_module(void)
{
printk(KERN_INFO "hi \n");
return 0;
}

void cleanup_module(void)
{
printk(KERN_INFO "bye \n");
}

**** makefile: *****

obj-m += hello-1.o

**** compile command: ***
gcc -DMODULE -D__KERNEL__ -isystem /lib/modules/2.6.17.7/build/include -W -O2 -c
hello-1.c

**** error *****
In file included from /usr/include/linux/module.h:10,
from hello-1.c:5:
/usr/include/linux/config.h:5:2: #error Incorrectly using glibc headers for a kernel module
hello-1.c: In function `int_module':
hello-1.c:10: error: `KERN_INFO' undeclared (first use in this function)
hello-1.c:10: error: (Each undeclared identifier is reported only once
hello-1.c:10: error: for each function it appears in.)
hello-1.c:10: error: syntax error before string constant
hello-1.c: In function `cleanup_module':
hello-1.c:16: error: `KERN_INFO' undeclared (first use in this function)
hello-1.c:16: error: syntax error before string constant

where is the problem????
please reply....'

Regards..
-- Ni3

Try like this: ***** Makefil

on
August 14, 2006 - 10:50am

Try like this:
***** Makefile *****
KDIR = /usr/src/linux/
SUBDIRS = $(PWD)
obj-m += hello-1.o
modules:
$(MAKE) -C $(KDIR) M=$(SUBDIRS) $@

***** hello-1.c *****
#include linux/init.h
#include linux/module.h
#include linux/kernel.h

MODULE_LICENSE("GPL");

static int __init init_module(void)
{
printk(KERN_INFO "hi \n");
return 0;
}
static void __exit cleanup_module(void)
{
printk(KERN_INFO "bye \n");
}

module_init(init_module);
module_exit(cleanup_module);

***** compile command *****
make modules

hello world module.....

on
August 21, 2006 - 4:59am

/* sir still there is a problem........, plz reply....*/

Makefile
***********
//Makefile

KDIR= /usr/src/linux-2.6.17.7/
SUBDIRS= /root/Desktop/test/
obj-m += hello-1.o
modules:
MAKE -C /usr/src/linux 2.6.17.7 M=/root/Desktop/test/
********
hello-1.c
*********
// hello-1.c
#include linux/init.h
#include linux/module.h
#include linux/kernel.h

MODULE_LICENSE("GPL");
9
static int __init init_module(void)
{
printk(KERN_INFO "hi \n");
return 0;
}
static void __exit cleanup_module(void)
{
printk(KERN_INFO "bye \n");
}

module_init(init_module);
module_exit(cleanup_module);

****************
error
*********
makefile 1 missing seperator. stop.
**********

How to solve this problem for loading and unloading kernel

virgoptrex (not verified)
on
September 28, 2008 - 9:38pm

Ok I am a beginner too. But I solved this problem. Used Ubuntu Hardy Heron 2.6.24.19 to build my own kernel 2.6.24.7-custom

Solution for this problem Build a kernel yourself and make sure you set the load/unload kernel in 'config' file in the following tutorial:

http://www.howtoforge.com/kernel_compilation_ubuntu

just replace 2.6.18.1 in the above tutorial everywhere by whatever kernel you would like. In my case I did 2.6.24.7

next after installing the kernel

follow the instructions below

if you have not set password for super user already then follow this otherwise go to the next step
-----------------
sudo passwd root
-----------------
type your password twice and "REMEMBER IT"
----------------------
su -
---------------------
Type the password
now your prompt should change to root
verify this by
----
pwd
----
now you need to port your "hello.c" file here

if you know the path/directory where this is situated do the following
e.g if its on your Desktop
----------
cp /home/xxxx/Desktop /root
----------

If you can't do this create hello.c.

-----------
nano hello.c
-----------

(you can also use vi editor if you are comfortable with it)
open this copy and paste driver code above and Don't forget to save it before exiting

----------
nano Makefile
----------

Type in the following
-----------------------
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
-------------------------
the command (shell uname -r) will automatically find kernel version for you

next save it and close it

then type
----
make
----

to see what types of files are created
------
ls -al
------
you should see file 'hello.o', 'hello.ko' etc
if everything goes well then you should see a prompt promptly without pc being freezing :)
now insert module
------
insmod ./hello.ko
------

The module should insert successfully if everything was done as per procedure

then you must unload the module

----
rmmod hello
----

To see the messages "hello world" and "goodbye world" or whatever you had
type
------
cat /var/log/messages
-----------

Thats it!!! It should give you a headstart!!!
Thanks to "gizmo","AlexOoO" and many others on the internet for their helpful posts!!!

Some comments to previous pos

on
August 14, 2006 - 12:50pm

Some comments to previous post:

You're trying to compile module using userspaces environment. It is reason of your error. You must show to compiler, where it can find kernelspace env (-C option of make), and place of your module (-M option).
Additionally, in latest branch of kernel (2.6.x), initialization and cleanup functions of module must be defined with macroses module_init and module_exit.
With this changes this compilation must be completed successfully.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.