3

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 
Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33
xj107359
  • 52
  • 1
  • 5
  • 1
    You can't use library code in kernel modules. See e.g. http://www.tldp.org/LDP/lkmpg/2.6/html/index.html . –  Dec 10 '13 at 06:24
  • In a sense, *.ko files are already dynamic libraries - one can create helper modules to be loaded on demand to provide necessary functionality (as it is done with crc, crypto modules and other stuff like that). – oakad Dec 10 '13 at 06:33
  • Thanks for you repley. kernel modules cann't call even Standard C functions in stdio.h. I'm not used to this way. Is it possible to write logic code in cpp with Boost and other libs, and ko to call the logic code ? – xj107359 Dec 10 '13 at 06:44
  • You mean write kernel code using c++? – moeCake Dec 10 '13 at 07:01
  • I mean write logic code using C++, compile logic code into a C Compatible static or dynamic library; Kernel module load the library, and call logic function. – xj107359 Dec 10 '13 at 07:07
  • 1
    *.ko files are relocatable files, right. I don't think are shared object files (dynamic libraries). – Shukant Pal Oct 22 '17 at 15:37
  • New updated link from user2845360 is https://sysprog21.github.io/lkmpg for updated kernel 5.x. – haxpor Sep 10 '21 at 19:36

1 Answers1

1

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.

sowo
  • 34
  • 3