Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-02-2009, 04:16 PM   #1
LQ Newbie
Registered: Feb 2009
Posts: 1

Rep: Reputation: 0
linking multiple files kernel module programming

I am just writing a test file and then i am trying to call a function inside my kernel.

Can somebody tell if i am making a mistake in my makefile or my code??

I wanted to call my int d = add(2, 4); function but the program does not seem to link test.c file with sourceadd2.c.

/* sourceadd2.c */

#define __KERNEL__
#define MODULE
#include "test.h"
#include <linux/ip.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>

unsigned int hook_func(unsigned int hooknum, struct sk_buff **skb, const struct net_device *in, const struct net_device *out, int (*okfn) (struct sk_buff *)){

int d = add(2, 4);

printk(KERN_INFO " testing %d \n", d);

return NF_DROP;


int init_module()
/* Fill in our hook structure */

nfho.hook = hook_func;

/* Handler function */

nfho.hooknum = NF_IP_PRE_ROUTING; /* First hook for IPv4 */ = PF_INET;

nfho.priority = NF_IP_PRI_FIRST; /* Make our function first */


return 0;

/* Cleanup routine */

void cleanup_module()

/* test.c */

#include "test.h"

int add(int a , int b)
return a+b;

/* test.h */

#ifndef __TEST_H__
#define __TEST_H__

int add(int a, int b);



obj-m :=sourceadd2.o

sourceadd2-objs := test.o

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

PWD := $(shell pwd)


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

I am getting following error -

WARNING: "amino"[/home/user1/Desktop/amin/sourceadd2.ko] undefined!

Old 02-17-2009, 12:28 PM   #2
Registered: Dec 2008
Location: CH/IL
Distribution: Slackware 13.1 Fedora 12
Posts: 73

Rep: Reputation: 16
spanning multiple files

Sometimes it makes sense to divide a kernel module between several source files. Lets say you have 2 source files :
start.c and stop.c
you can create a Makefile like this :

obj-m += startstop.o
startstop-objs := start.o stop.o

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean


files, kernel, linking, module, multiple

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
linking .o files built in user space against kernel module using kbuild santigopal_mondal Linux - Newbie 1 01-03-2009 02:42 PM
Linking precompile archive/object files with kernel module possible? Mechanic Linux - General 0 08-25-2006 02:08 PM
Kernel module programming Error : module not found coolguy_iiit Programming 1 07-30-2006 07:23 AM
Kernel module with multiple source files compilation problem Last Attacker Programming 7 05-06-2006 09:10 AM
creating Makefile with multiple kernel module files b123coder Programming 0 12-26-2004 08:41 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:19 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration