c - 对 `__gmpz 和 makefile 的 undefined reference

标签 c makefile compilation undefined

我不明白为什么会出现这些编译错误:

gcc -o Compil obj/MillerRabin.o obj/main.o -L. -L/usr/lib/ -Wl,-rpath=/home/---------/Documents/AC20_Cyrpto -lgmp  -lCommon  -ltest  -lprimeGen
./libtest.so: undefined reference to `__gmpz_set_str'
./libtest.so: undefined reference to `__gmpz_set_ui'
./libtest.so: undefined reference to `__gmpz_add_ui'
./libtest.so: undefined reference to `__gmpz_clear'
./libtest.so: undefined reference to `__gmpz_init'
./libtest.so: undefined reference to `mpzPrint'
collect2: error: ld returned 1 exit status
Makefile:61: recipe for target 'Compil' failed
make: *** [Compil] Error 1

这些错误来自libs/test/test.c,我正在使用我的一个 friend 制作的make文件,而且我还没有真正研究它们是如何工作的。

所以奇怪的是,在我的 libs 文件夹中,我有另一个使用 gmp 的 .c 文件(libs/Common/common.hmpzfunctions.c ),并且那个不会给我任何错误。如果我将测试函数移至主函数,编译就会起作用。

如果有人知道如何解决这个问题,那就太好了!谢谢。

最佳答案

库中的符号按顺序应用。所以当你写:

-lgmp -ltest

这意味着 libgmp 中使用的符号将在 libtest 中查找,但反之则不然。

显然,您的 libtest 需要 libgmp 中的函数,因此您需要以相反的顺序应用它们。

根据经验,外部库应该始终放在最后,因为它们通常不需要您库中的符号。

所以在不知道太多的情况下,我猜顺序应该是:

 -lCommon -lprimeGen -ltest -lgmp

在我看来,您会想要从 LINK_FLAGS 中删除 -lgmp 并将其以某种方式添加到链接命令的末尾。

关于c - 对 `__gmpz 和 makefile 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49329414/

相关文章:

c - 在头文件中定义变量 - C

C 计算输入字符数

c++ - 使用 Makefile 的旧 C++ 项目..迁移到 Eclipse IDE?

c - 多线程进程的最大堆栈大小

c++ - 有什么好的教程可以帮助我创建用于 C、C++ 中随机化的头文件

C: undefined reference `dlopen`/`dlsym` 尽管添加了 `-ldl` 标志

makefile - 从 Makefile 中的路径名中获取子字符串

检查文件是否是c文件并编译它

perl - 如何在 FreeBSD 7.1 下编译具有线程支持的 Perl 5.10?

java - 编译 Java 类并从命令控制台运行 Java 文件时,如何包含 Java jar 文件?