内核模块中的 GMP

问题描述 投票:0回答:2

我对模块编码很陌生,我需要在模块中运行一些使用 GMP 库的计算。

所以第一个问题:通常可以在内核中运行 GMP 吗? 为了测试,我编写了这个模块:

#include <linux/init.h>
#include <linux/module.h>
#include <gmp.h>

int hallo_init(void)
{
   mpz_t testFactor;
   mpz_init( testFactor, NULL);
   mpz_set_str(testFactor, "19", 10);
   int length = (int) mpz_sizeinbase(testFactor,2);

   printk(KERN_ALERT "That is testFactor: %x \n",length);

   return 0;
}

void hallo_exit(void)
{
   printk(KERN_ALERT "exit \n");
}

module_init(hallo_init);
module_exit(hallo_exit);

我使用以下命令运行它:

sudo make -C /lib/modules/$(uname -r)/build M=$PWD modules -lgmp

makefile 包含

obj-m := gmpFile.o

我还尝试在makefile中使用-lgmp:

obj-m := halloGmp.o
ccflags-y := -lgmp

但我总是遇到致命错误:

gmp.h: No such file or directory
有什么建议么?非常感谢您的帮助!

c makefile module kernel gmp
2个回答
0
投票

我不熟悉 GMP,但你不太可能将库动态链接到内核模块。

原因是内核是一个独立的程序,不知道您使用的任何系统库(例如 glib...),并且很可能 GMP 使用这些库。

我能想到的唯一解决方案是创建一个与用户空间中的程序通信的内核模块,并将 GMP 链接到应用程序的用户空间部分。


0
投票

这里唯一的答案来自一个“不熟悉 GMP”的人,这很烦人。 是的 随 gcc 一起提供,而不是内核,因此您不能将 gmp 用于内核模块,因为您无法链接它。

我遇到了类似的问题,我需要比 unsinged long long 更大的数字,而 Linux 内核中似乎没有等效的数字。 如果我找到一个,我会编辑此评论,或者如果有人知道,请在这篇文章上发表评论。

© www.soinside.com 2019 - 2024. All rights reserved.