Benutzer-Werkzeuge

Webseiten-Werkzeuge


linux:kernel_programming

Link a Library to a Kernel Modul

We have the structure:

|
|-Makefile
|-hello.c
|-hello_lib.c
|-hello_lib.h

hello_lib.h:

int printHello(int);

hello_lib.c

int printHello(int count)
{
   int i;
   
   for(i=0;i<=count;i++)
   {
      printk("Hello World\n");
   }
   
   return 0;
}

hello.c:

#include <linux/kernel.h>
#include <linux/module.h>
#include "hello_lib.h"

MODULE_LICENSE("GPL");

int init_module(void)
{
   printk("call function\n");
   printHello(5);
   return 0;
}

void cleanup_module(void)
{
   printk(KERN_INFO "remove module\n");
   return;
}

Makefile:

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

obj-m += test.o
test-y := hello.o libhello_lib.a

all:
	gcc -I/usr/include -c -o hello_lib.o hello_lib.c
	rm -f libhello_lib.a
	ar cru libhello_lib.a hello_lib.o
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules KBUILD_VERBOSE=1

clean:
	rm -f *.o
	rm -f *.ko
	rm -fr .tmp_versions
	rm -f Modules.symvers
	rm -f *.a
	rm -f *.mod.c
	rm -f .*.cmd

Test it with:

insmod ./test.ko

Remove the module with:

rmmod test

Maybe you want this here:

insmod ./test.ko && rmmod test
linux/kernel_programming.txt · Zuletzt geändert: 2013/12/16 12:52 von idefix