如何将静态或共享库链接到内核模块? [英] How to Link static or shared library to Kernel Module?
问题描述
aaa.c中有一个功能
There is a function in aaa.c
int myadd(int a, int b){
return a+b;
}
并且使用
gcc -c aaa.c -o aaa.o&& ar -cr libaaa.a aaa.o
gcc -c aaa.c -o aaa.o && ar -cr libaaa.a aaa.o
和使用
gcc -c aaa.c -o aaa.o&& gcc -shared -fPCI -o libaaa.so aaa.o
gcc -c aaa.c -o aaa.o && gcc -shared -fPCI -o libaaa.so aaa.o
然后我写了一个文件call.c,尝试在libaaa.so中调用函数myadd()
,但是失败了.
Then I wrote a file call.c, and try to call function myadd()
in libaaa.so, but failed.
请给我一些建议,
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");
此Makefile会将两个c文件都放入call.ko中,并且可以正常工作.但这不是我想要的. 生成文件:
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
推荐答案
Ko文件在内核空间中运行,而不是在运行应用程序的用户空间中运行. Libc或libc ++等为用户空间应用程序准备.因此,您无法链接libc/c ++函数,就像无法在内核中链接任何libc函数一样.
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.
这篇关于如何将静态或共享库链接到内核模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!