LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   compile a hello world kernel mod. (https://www.linuxquestions.org/questions/programming-9/compile-a-hello-world-kernel-mod-409290/)

slzckboy 01-29-2006 09:34 AM

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. :o
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.

nx5000 01-30-2006 03:03 AM

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


paragn 01-30-2006 04:08 AM

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

slzckboy 01-31-2006 03:22 AM

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.

nx5000 01-31-2006 04:07 AM

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

slzckboy 02-02-2006 08:58 AM

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.

:confused:

where does hello2.o come from ?

nx5000 02-06-2006 01:10 AM

I intentionnaly introduce a typo to see if you were following ;)
its not hello2.o but hello.o

edit: error in post #2 corrected

slzckboy 02-06-2006 05:29 AM

:cool:

Good answer.


All times are GMT -5. The time now is 02:30 AM.