LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-29-2006, 09:34 AM   #1
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Rep: Reputation: 30
compile a hello world kernel mod.


hi I am following linux device drivers 3rd edition. running a 2.6.14 kernel in slackware 10.2 I have written the obligatory hello world program but,I can't get it to compile... i tried
Code:
 obj-m := hello.o
as recommended by the book but gcc complained that there are no targets. i then trid to hash together a more conventional makefile but err.. well,I'm not too familiar with make.
Code:
 CC=gcc CFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX INCLUDES := /usr/include hello.o: hello.c $(CC) $(CFLAGS) -c hello.c
this is the hello world prog.
Code:
 #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void){ printk(KERN_ALERT"Who's Your Daddy?\n"); return 0; } static void hello_exit(void){ printk(KERN_ALERT"Bye,Bye\n"); } module_init(hello_init); module_exit(hello_exit);
any pointers would be appreciated.

Last edited by slzckboy; 01-29-2006 at 03:55 PM.
 
Old 01-30-2006, 03:03 AM   #2
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
Quote:
Originally Posted by slzckboy
well,I'm not too familiar with make.
Hello,
I think you are skipping some steps. You should learn a bit on make with not-kernel code.
Anyway, you can help yourself by inserting your module in the kernel source tree, so you don't have to bother with makefile and the options passed to GCC for creating a module example:
put your source file in /home/me/ldd3
call it hello.c then
Code:
cd /usr/src/linuxxxx
cd drivers
ln -s /home/me/ldd3
Tell the makefile that there is a module directory in ldd3 that has to be added to the list:
Code:
echo "obj-m += ldd3" >> Makefile
Create a dummy makefile in this directory.
You add hello2.o to the list (+=) of modules the top makefile has to compile.
Code:
cd ldd3
echo "obj-m += hello.o" > Makefile
now go back to the kernel root, it is where you can issue make modules and the Makefile will descent into the whole directories and compiles the list of modules (obj-m).
Code:
cd /usr/src/linux
make modules
cd drivers/ldd3
insmod ./hello.ko
tail /var/log/kern.log

Last edited by nx5000; 02-06-2006 at 07:48 AM.
 
Old 01-30-2006, 04:08 AM   #3
paragn
Member
 
Registered: Jan 2006
Distribution: Red Hat EL5, Fedora 7
Posts: 259

Rep: Reputation: 30
hi,
To compile a kernel module in 2.6 kernel you need Makefile

Quote:
obj-m := hello.o

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

all:
$(MAKE) -C $(KERNELDIR) M=$(PWD)

clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
 
Old 01-31-2006, 03:22 AM   #4
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Original Poster
Rep: Reputation: 30
thnks gents.

After reading your posts and then doing a bit more reading
my first modules have been built on both 2.4* and 2.6*

I'm not totally unfamiliar with make but it is a weakness that I'm working on.
 
Old 01-31-2006, 04:07 AM   #5
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
I come back on what I said, make is important this is true.
But the makefile of the kernel is quite complex, its syntax is very optimized and you will get a big headache reading it.

If you put your files in the source tree and use the makefile of the kernel guys, the parameters of gcc will always be correct among different kernels (still the module interface has changed, you might need the file from allessandro sysreference.h or something like this) and you probably don't need to look so much in the makefiles.
You can concentrate on driver coding which is also a very tough task. Some kernel coding needs an average level, others need a very good understanding of the whole thing.
good luck
 
Old 02-02-2006, 08:58 AM   #6
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Original Poster
Rep: Reputation: 30
true I would very much prefer to concentrate on Kernel code and understanding how the computers resources are made available to programs via the kernel etc..
but I have a question..

Quote:

cd ldd3
echo "obj-m += hello2.o" > Makefile
I don't understand the reference to hello2.o.



where does hello2.o come from ?
 
Old 02-06-2006, 01:10 AM   #7
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
I intentionnaly introduce a typo to see if you were following
its not hello2.o but hello.o

edit: error in post #2 corrected

Last edited by nx5000; 02-06-2006 at 07:49 AM.
 
Old 02-06-2006, 05:29 AM   #8
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Original Poster
Rep: Reputation: 30


Good answer.
 
  


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
yum update kernel cant find ATI mod true_atlantis Fedora 2 09-30-2005 03:47 AM
compile apache source rpm with mod rewrite robertngo Linux - Software 3 07-21-2005 03:44 AM
which kernel mod and driver? ATI 9600se cjae SUSE / openSUSE 1 04-16-2005 09:37 AM
install kernel mod for smc9425tx or Dlink DGE500T dovkruger Linux - Networking 2 10-19-2004 10:16 AM
RH8/9 kernel panic SCSI mod - newbie roffer Linux - Hardware 4 06-15-2003 07:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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