kernel module spanning multiple files

amit mehta gmate.amit at gmail.com
Wed Jun 15 05:59:52 EDT 2011


Hi,

After looking at some Makefiles under Linux Driver sources
which uses multiple files to create the target, i tried to write
a simple hello world kernel module spanning multiple files for learning purpose.
But I'm observing a strange behavior. After inserting the module,
I don't see the prints getting logged into the syslog(/var/log/message)
or in the dmesg. Maybe its just a C programming mistake, or is due to
an incorrect Makefile but as of now I'm clueless.

Here are the code snippet from the four files(hello_multi.c, helper.c,
myhdr.h, Makefile)

[root at localhost #] cat hello_multi.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include "myhdr.h"
static int __init hello_init(void)
{
	printk(KERN_WARNING "Hello world\n");
	foo(); //defined in anther file
	return 0;
}

static void __exit hello_exit(void)
{
	printk(KERN_WARNING "goodbye world\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL v2");

[root at localhost #] cat helper.c
#include <linux/kernel.h>
#include "myhdr.h"
void foo(void)
{
	printk(KERN_WARNING "inside foo\n");
}

[root at localhost #] cat myhdr.h
#ifndef __MY_HDR_H
#define __MY_HDR_H
void foo(void);
#endif

[root at localhost #] cat Makefile
obj-m += hello_multi.o
hello_multi-objs := helper.o
KERNELDIR=/lib/modules/$(shell uname -r)/build
PWD=$(shell pwd)

modules:
	make -C $(KERNELDIR) M=$(PWD) modules

modules_install:
	make -C $(KERNELDIR) M=$(PWD) modules_install

clean:
	rm -rf *.o *.ko *~ core .depend .tmp_versions .*.cmd *.mod.c
.PHONY: modules modules_install clean

Invoking 'make modules' completes with success and i am able to
insert this module.

[root at localhost #]  make modules
make -C /lib/modules/2.6.31-23-generic/build
M=/home/superman/programming/ldd/hello_multi modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.31-23-generic'
  CC [M]  /home/superman/programming/ldd/hello_multi/helper.o
  LD [M]  /home/superman/programming/ldd/hello_multi/hello_multi.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/superman/programming/ldd/hello_multi/hello_multi.mod.o
  LD [M]  /home/superman/programming/ldd/hello_multi/hello_multi.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.31-23-generic'
[root at localhost #] dmesg -c
[root at localhost #] insmod hello_multi.ko
[root at localhost #] dmesg|tail    <-- -- No messages here

[root at localhost #] cat /proc/modules |grep -i hello
hello_multi 727 0 - Live 0xffffffffa020b000 (PN

[root at localhost #] nm hello_multi.ko
0000000000000000 r ____versions
0000000000000000 r __mod_srcversion26
0000000000000040 r __mod_vermagic5
0000000000000023 r __module_depends
0000000000000000 D __this_module
0000000000000000 T foo
                 U printk

Regards,
amit



More information about the Kernelnewbies mailing list