我不明白为什么会出现这些编译错误:
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.h
和 mpzfunctions.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/