Kernel module compilation problem

I have one big module 2.6.18 kernel mod.c
I want to divide this to several files.
The problem is to write right Makefile

lib1.h
lib1.c

mod.c

mod.c works fine normally but when I divide into several files
and try to compile with this makefile

obj-m := mod.o
mod-objs := lib1.o

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

default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

I get this

zet modulANDlibs # make
make -C /lib/modules/2.6.18-gentoo-r2/build SUBDIRS=/root/modulANDlibs modules
make[1]: Entering directory `/usr/src/linux-2.6.18-gentoo-r2'
CC [M] /root/modulANDlibs/lib1.o
LD [M] /root/modulANDlibs/mod.o
Building modules, stage 2.
MODPOST
CC /root/modulANDlibs/mod.mod.o
LD [M] /root/modulANDlibs/mod.ko
make[1]: Leaving directory `/usr/src/linux-2.6.18-gentoo-r2'
zet modulANDlibs #

so this is ok but when I load this module there are no any operation
this mean mod.ko is loaded but dont work - before dividing it print to KERN_INFO "Hello world" ( dmesg )

There is one more thing. When I compile mod.c without lib* mod.ko have about 5.5Kb
but if I compile with mod-objs variable doesnt matter what is in mod.c and in lib1.c
compilation is ok loading is ok and size mod.ko is about 1.6 Kb constatntly

The questin is WHY ? :slight_smile:

The thing is, every .c file normally is compiled separately into a separate object file. Unlike normal programs, there is no linker to combine the object files. If you want the kernel to load all these files, you have to make *each* of them a kernel module.

What you can instead do is something like this: split your code into two separate files, mod.c and lib.c, and then inside mod.c you have:

#include "lib.c"

You can put it at the top or at the end. Keep in mind, globals or static data defined in one .c file will be seen in the other.

You are wrong :slight_smile:
I found the solution using kbuild system in 2.6 kernel

obj-m := name.o
name-objs := lib1.o mod.o

Dear Marcintom,

Just saw your post on compiling the kernel modules spanning multiple files. I am not able to do it .
I have one file mod.c (the main kernel module) and one list.c (which is the library)
Could you please help me with the following questions ..
1) Is a header file needed (list.h) to be included in the mod.c ?
2) do we need to write the list.c in the kernel mode (using #define __kernel___) and using kmalloc etc ?
3) How would you write the make file ?

Right now If i try writing list.c in the kernel mode and use the makefile that u have give it give a lot of compile errors and if i write it in the user mode it give un known symobol while loading the module somehow it is not able to link it..it would be great if you could please help.

Natraj