LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-14-2011, 03:17 AM   #1
mbschenkel
LQ Newbie
 
Registered: Jun 2011
Posts: 2

Rep: Reputation: Disabled
Post Kernel Module with multiple source files not initializing


Hi,

I am trying split up a kernel module into multiple c files. However I either get missing symbols at compile or load time or when I think everything is correct, the module just doesnt start at all. It appears as loaded but my init isn't called. The following minimal example (Tested with Kernel 2.6.32 on Ubuntu) compiles without warnings, loads and unloads without any message, in particular I never get to see the printks.
As soon as I remove the second source file "my_include.h / .c" everything works as expected (ie. I see the init and exit in dmesg).

So, the main problem is, that I dont get any message at all and dont know how to approach that problem... Any hints would be appreciated!

Thanks,
Markus

Code:
// ------------ my_module.c ------------

#include <linux/module.h>
#include <linux/kernel.h>   /* PRINTK() */

#include "my_include.h"    /* our include */

MODULE_AUTHOR("Markus Schenkel");
MODULE_LICENSE("GPL");

void __exit my_module_cleanup(void) {
    printk(KERN_ALERT "MY_MODULE exit\n");
}

int __init my_module_init(void) {
    printk(KERN_ALERT "MY_MODULE init\n");
    my_helper();
    return 0;
}

module_init(my_module_init);
module_exit(my_module_cleanup);

Code:
//------------ my_include.h ------------

#ifndef __MY_INCLUDE_H__
#define __MY_INCLUDE_H__

void my_helper(void);

#endif
Code:
//------------ my_include.c ------------

#include <linux/kernel.h>   /* PRINTK() */
#include "my_include.h"

void my_helper(void) {
    printk(KERN_ALERT "MY_MODULE init\n");
}

Code:
#------------ Makefile ------------

EXTRA_CFLAGS += -O2
EXTRA_CFLAGS += -Wall
EXTRA_CFLAGS += -D__KERNEL__
EXTRA_CFLAGS += -DMODULE

ifneq ($(KERNELRELEASE),)
# call from kernel build system

obj-m   :=  my_module.o
my_module-objs := my_include.o

else

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

modules:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINC=$(PWD) modules

endif

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

depend .depend dep:
	$(CC) $(EXTRA_CFLAGS) -M *.c > .depend

ifeq (.depend,$(wildcard .depend))
include .depend
endif
 
Old 06-20-2011, 09:58 AM   #2
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Rep: Reputation: 24
plz read the below articles first

http://tldp.org/LDP/lkmpg/2.4/html/x385.html
http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html#AEN351
 
Old 06-20-2011, 03:45 PM   #3
mbschenkel
LQ Newbie
 
Registered: Jun 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thumbs up

Thanks for your reply. I had actually solved this problem the other day and I think that although this start-stop example contains the solution, it had also confused me. Because I didnt want to split my init/exit functions, but just include some existing code I ignored it somewhat in that I still had a "my_module.c" which was at the same time the name of the module itself (ie. obj-m := my_module.o). But when I changed it to something else it worked just well. For completeness:

Code:
#...
obj-m   :=  my_module_name.o
# Note that my_module_name.o != my_module.o and that my_module_name.c does NOT exist !!!
my_module-objs := my_module.o my_include.o
# and simply added the top level module here
#...
Not sure whether this is completely how it is meant to be used, but it works :-)
Without changes to the C code or __NO_VERSION__ declarations whatsoever with Kernel 2.6

Best,
Markus
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
loding Kernel module with multiple source files nayankumarp Programming 0 10-23-2009 02:41 PM
linking multiple files kernel module programming charlotteix9 Programming 1 02-17-2009 01:28 PM
Kernel module with multiple source files compilation problem Last Attacker Programming 7 05-06-2006 10:10 AM
creating Makefile with multiple kernel module files b123coder Programming 0 12-26-2004 09:41 AM
Building kernel module from multiple source file in 2.6 kernel yogeshwar_s Programming 1 12-20-2004 10:31 AM

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

All times are GMT -5. The time now is 03:50 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration