-   Programming (
-   -   linking multiple files kernel module programming (

charlotteix9 02-02-2009 04:16 PM

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!


chymeira 02-17-2009 12:28 PM

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

All times are GMT -5. The time now is 04:37 PM.