LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 10-02-2007, 01:57 PM   #1
sniffer_raghav
Member
 
Registered: Oct 2007
Posts: 34

Rep: Reputation: 15
Question kernel module programming


hiiii every body i am just a beginner in kernel module programming.
i want help regarding it .
i have written a simple module and trying to complie it but it is giving the error that linux/module.h file is not there though i have seprately specified the pat of file but then dependencies are missing please help me out......
thanx in advance.

Last edited by sniffer_raghav; 10-02-2007 at 01:58 PM.
 
Old 10-02-2007, 08:22 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Perhaps you should begin by studying a small existing module.

No, seriously...
 
Old 10-03-2007, 01:53 PM   #3
sniffer_raghav
Member
 
Registered: Oct 2007
Posts: 34

Original Poster
Rep: Reputation: 15
Question

hiii....
i have written a simple module which is as follows
#include <linux/module.h>

int init_module(void)
{
printk("<1>Hello, world\n");
return 0;
}
void cleanup_module(void)
{
printk("<1>Goodbye cruel world\n");
}
first the error was that linux/module.h
file not found ..............
then i gave the path where file is but it is giving dependencies error .........
can u suggest me any tutorial or sol of this prob ..........
thanx in advance......
 
Old 10-04-2007, 11:11 AM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by sniffer_raghav View Post
first the error was that linux/module.h
file not found ..............
then i gave the path where file is but it is giving dependencies error .........
can u suggest me any tutorial or sol of this prob ..........
thanx in advance......
Well, you haven’t told us how you are building this module. Since the error is occurring during building, it would seem this is an important detail…

In any case, the recommended method of compilation is to use the kernel’s own build system. E.g., for a simple, one-file module try something like this:
Code:
make -C "/lib/modules/$(uname -r)/build" M=$PWD obj-m="$(basename $filename .c).o"
(where $filename is the name of your C sourcefile).

You can read more about the 2.6 kernel build system in just about any kernel programming guide or tutorial.
 
Old 10-06-2007, 03:20 AM   #5
sniffer_raghav
Member
 
Registered: Oct 2007
Posts: 34

Original Poster
Rep: Reputation: 15
Question

i m buliding this module by following set of command
first im creating object file by
#gcc -c -i hello.c
during compilation its is giving error

and the command u have suggested i have tried
it is giving this >
now what i have to supply ????????/
plz reply..........
thanx in advance
 
Old 10-06-2007, 10:40 AM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by sniffer_raghav View Post
i m buliding this module by following set of command
first im creating object file by
#gcc -c -i hello.c
Well, that’s your problem then…
Quote:
Originally Posted by sniffer_raghav View Post
and the command u have suggested i have tried
it is giving this >
I suggest you carefully look at the command suggested, and make sure you copy-and-paste it correctly. For example, if my file is called hello.c, this is what I do:
Code:
filename=hello.c
make -C "/lib/modules/$(uname -r)/build" M=$PWD obj-m="$(basename $filename .c).o"
In any case, I would read (yes, as in ) any linux 2.6 kernel programming guide (or even the official document), for generic build instructions.
 
Old 10-09-2007, 10:54 PM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
By all means, look at the literally-hundreds of existing modules that are available in your Linux distro! (You might have to install "source" packages to see them.)

The most common way to approach any kernel-module is to find something that's "almost" what you want, then practice installing it, and then use it as a starting-point. "If you are starting from scratch, on anything, you are doing something wrong."
 
Old 10-31-2007, 01:24 PM   #8
sniffer_raghav
Member
 
Registered: Oct 2007
Posts: 34

Original Poster
Rep: Reputation: 15
Question

thank you sir ,
sir i have tried wat you told me to do ...........
again i am getting error that is given below...........
make -C "/lib/modules/$(uname -r)/build" M=$PWD obj-m="$(basename $hello .c).o"
make: Entering directory `/usr/src/kernels/linux-2.6.23'
LD /root/device/built-in.o
make[1]: *** No rule to make target `/root/device/.c.c', needed by `/root/device/.c.o'. Stop.
make: *** [_module_/root/device] Error 2
make: Leaving directory `/usr/src/kernels/linux-2.6.23'

please read it and reply.......
i have also compiled a new kernle 2.6.23
and have includede all the kernel headers .......
though my prob is not solved...........
please help me out ...........
 
Old 10-31-2007, 09:33 PM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by sniffer_raghav View Post
Code:
make -C "/lib/modules/$(uname -r)/build" M=$PWD obj-m="$(basename $hello .c).o"
Hmmm… What could be the problem?
Quote:
Originally Posted by sniffer_raghav View Post
please read it and reply.......
It doesn’t look like you’ve looked at any of the seven links I had in my last post. In fact, you barely managed to read my post (you seem to have missed an entire line).
 
Old 11-01-2007, 03:12 AM   #10
sniffer_raghav
Member
 
Registered: Oct 2007
Posts: 34

Original Poster
Rep: Reputation: 15
sir i have read the pdf you have provided and followed the things given dere .........
but prob is not solved the prob i m getting is below.............
make -C "/lib/modules/$(uname -r)/build" M=$PWD obj-m="$(basename $hello .c).o"
make: Entering directory `/usr/src/kernels/2.6.21-1.3194.fc7-i686'
make[1]: *** No rule to make target `/root/device/.c.c', needed by `/root/device/.c.o'. Stop.
make: *** [_module_/root/device] Error 2
make: Leaving directory `/usr/src/kernels/2.6.21-1.3194.fc7-i686'
please sir tell y dis error is occuring ..........
thanx for the material provided ...........
 
Old 11-01-2007, 12:26 PM   #11
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
First, did you not see the highlighted string from my previous post?
Quote:
Originally Posted by sniffer_raghav View Post
sir i have read the pdf you have provided and followed the things given dere .........
but prob is not solved the prob i m getting is below.............
make -C "/lib/modules/$(uname -r)/build" M=$PWD obj-m="$(basename $hello .c).o"
Had you read any of those articles, you would have found that the obj-m should hold a list of object files which shall be linked to be loadable kernel object files. The dependency resolution shall assume (this assumption may be overridden) that the sole source file corresponding to said object file has the same basename (without extension) and has extension “.c”.

So if you want a kernel object file named “foo.ko”, you would set “obj-m=foo.o”, and the kernel build system would compile such an object from the file “foo.c”. Conversely, if you have a source file named “bar.c” and wanted to compile it, you would set “obj-m=bar.o”, and the end-result would be a file named “bar.ko”.

Now, suppose you have a file named “hello.c”. How do you go about compiling it? You could do this:
Code:
hello=hello.c
make -C "/lib/modules/$(uname -r)/build" M=$PWD obj-m="$(basename $hello .c).o"
Or this:
Code:
make -C "/lib/modules/$(uname -r)/build" M=$PWD obj-m="$(basename hello.c .c).o"
Or even this:
Code:
make -C "/lib/modules/$(uname -r)/build" M=$PWD obj-m="hello.o"
 
Old 11-02-2007, 03:03 AM   #12
sniffer_raghav
Member
 
Registered: Oct 2007
Posts: 34

Original Poster
Rep: Reputation: 15
Smile

sir,
i have tread all those links and was successful in loading and
umloading the module. the problem is solved.now when i have written a module and loaded it tell me the next step to follow for learning the programming.
thank you for the co operation.
 
Old 11-02-2007, 09:24 AM   #13
dale_chip
Member
 
Registered: Jun 2007
Location: India
Posts: 41

Rep: Reputation: 15
hello ,
coincidentally i was trying the same thing on 2.6.23 kernel
i did exactly
make -C "/lib/modules/$(uname -r)/build" M=$PWD obj-m="helloworld.o"

my file name was helloworld.c
and $PWD was /root

file helloworld.c is :
#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);

as taken from a e-book on module and device drivers programming for the 2.6.* kernels.

but i was getting errors :
make: Entering directory `/usr/src/linux-2.6.23.1'
scripts/Makefile.build:33: /root/Makefile: No such file or directory
make[1]: *** No rule to make target `/root/Makefile'. Stop.
make: *** [_module_/root] Error 2
make: Leaving directory `/usr/src/linux-2.6.23.1'

please can any one help me out ...
 
Old 11-02-2007, 09:35 AM   #14
dale_chip
Member
 
Registered: Jun 2007
Location: India
Posts: 41

Rep: Reputation: 15
i tried out this thing :
i made a makefile :
obj-m := helloworld.o

then i did this :
make -C /usr/src/linux SUBDIRS=$PWD modules

it also gave errors

but when i did cp makefile Makefile
it compiled succesfully.

why did it not happen wid
make -C "/lib/modules/$(uname -r)/build" M=$PWD obj-m="helloworld.o"

plz someone explain this to me, cos i feel its understanding is crucial for further study in this field.
 
Old 11-03-2007, 03:26 AM   #15
sniffer_raghav
Member
 
Registered: Oct 2007
Posts: 34

Original Poster
Rep: Reputation: 15
Smile

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
static int hello3_data __initdata = 3;
static int __init hello_3_init(void)
{
printk(KERN_INFO "Hello, world %d\n", hello3_data);
return 0;
}
static void __exit hello_3_exit(void)
{
printk(KERN_INFO "Goodbye, world 3\n");
}
module_init(hello_3_init);
module_exit(hello_3_exit);

sir i have successfully compiled the above code but problem is that
printk is not giving message on the terminal ......
y is it so sir explain ...........
thank you..........
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kernel module programming Error : module not found coolguy_iiit Programming 1 07-30-2006 07:23 AM
Kernel module programming lokutas Programming 2 04-27-2004 02:26 AM
Kernel Module Programming in 2.6 KneeLess Programming 3 04-13-2004 02:27 PM
Kernel Module programming Config Programming 2 03-24-2004 04:48 PM
Kernel Module Programming Thomas.P Programming 1 11-10-2001 10:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 05:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration