如何将静态或共享库链接到内核模块
How to Link static or shared library to Kernel Module?
在aaa.c 中有一个函数
int myadd(int a, int b){
return a+b;
}
并且使用将aaa.c构建到静态库中
gcc-c aaa.c-o aaa.o&;ar-cr libaaa.a aaa.o
以及使用的共享库
gcc-c aaa.c-o aaa.o&;gcc-共享-fPCI-o libaaa.so aaa.o
然后我编写了一个文件call.c,并尝试调用libaaa.so中的函数myadd()
,但失败了。
,请给我一些建议
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 Destinyn");
c = myadd(1, 2);
printk(KERN_ALERT "res is (%d)n", c);
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "goodbye,kerneln");
}
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中,它就可以工作了。但那不是我想要的。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函数一样。
相关文章:
- 链接节点本机模块中的提升库,对象文件需要替代库版本
- 如何链接两个LLVM位码模块?
- 如何将 Pybind11 与 CMAKE 一起使用以链接 2 个模块
- 节点本机模块 - 链接静态库
- CMake - 将静态 yaml-cpp 作为 git 子模块的链接器错误
- 哪种方法更好获取静态链接DLL的模块句柄(HMODULE) - 使用GetModuleHandleEx()或LoadLibrary()?
- OpenCV 模块作为符号链接会导致 cmake 错误
- 理解TensorFlow中的Op注册和内核链接
- 静态Qt链接版本,Qt 5.2,QT中的未知模块:快速qml
- 如何通过智能链接制作基于模块的程序
- 如何将静态或共享库链接到内核模块
- 清除Linux内核链接列表
- 尝试在cpp模块中使用汇编程序代码时发生链接器错误
- 内置和静态链接的升压模块会导致 shutdown_service() 出现一堆链接器错误
- 将自定义 Apache 2.4 模块与 Linux 上的 httpd/apr 库静态链接时出错
- 当我在 opencv 3.1.0 中包含 dnn 模块时出现链接错误
- 将几个LLVM C++模块链接在一起会在lli中产生segfault
- 将Node.js模块中的Boost与Node-gip链接
- 链接内核后跳转到保护模式不工作
- 从链接中删除库模块