xj107359
xj107359

Reputation: 50

How to Link static or shared library to Kernel Module?

There is a function in aaa.c

  int myadd(int a, int b){ 
        return a+b;
  }

and aaa.c was built into a static library using

gcc -c aaa.c -o aaa.o && ar -cr libaaa.a aaa.o

and a shared library using

gcc -c aaa.c -o aaa.o && gcc -shared -fPCI -o libaaa.so aaa.o

Then I wrote a file call.c, and try to call function myadd() in libaaa.so, but failed.

Please give me some advice,

test.c:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE("Dual BSD/GPL");
extern int myadd(int a, int b);
static int hello_init(void)
{
    int c = 0;
    printk(KERN_ALERT "hello,I am Destiny\n");
    c = myadd(1, 2);
    printk(KERN_ALERT "res is (%d)\n", c);
    return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT "goodbye,kernel\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR("Destiny");
MODULE_DESCRIPTION("This is a simple example!\n");
MODULE_ALIAS("A simplest example");

This Makefile will make both c file into call.ko, and it will work. But that's not what I want. Makefile :

KVERSION = $(shell uname -r)

obj-m       = call.o
call-objs   = aaa.o test.o

Debug:
    make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules

All:Debug

cleanDebug:
    make -C /lib/modules/$(KVERSION)/build M=/home/Destiny/myProject/kernel/cbtest/ clean

clean:cleanDebug

installDebug:Debug
    rmmod /lib/modules/2.6.18-348.12.1.el5/test/call.ko
    /bin/cp call.ko /lib/modules/$(KVERSION)/test/
    depmod -a
    insmod /lib/modules/2.6.18-348.12.1.el5/test/call.ko

install:installDebug

main.o : defs.h 

Upvotes: 3

Views: 6083

Answers (1)

sowo
sowo

Reputation: 34

Ko files are running in kernel space , not user space where application running. Libc or libc++ and so on on are prepared for user space application. So you can not link libc/c++ functions, Just like you cannot link any libc functions in kernel.

Upvotes: 1

Related Questions