在 Linux 上静态链接 libgmp 时出错
GMP 文档表示静态链接可能会带来较小的性能改进。
我在让它在我的 Linux 系统上静态链接 libgmp 时遇到问题。我已将遇到的问题缩小到一个小测试用例。
gmptest.c
#include <gmp.h>
int main(int argc, char** argv) {
mpz_t foo;
mpz_init(foo);
return 0;
}
Makefile:
all: clean gmptest static
clean:
rm -f *.s
rm -f *.o
rm -f gmptest
rm -f static-gmptest
gmptest: Makefile gmptest.c
gcc -std=c99 -O3 -lgmp gmptest.c -o gmptest
static: clean Makefile gmptest.c
gcc -std=c99 -O3 -static /usr/lib/libgmp.a gmptest.c -o static-gmptest
非静态二进制文件编译和链接没有任何问题,但“Make static”产生:
gcc -std=c99 -O3 -static /usr/lib/libgmp.a gmptest.c -o static-gmptest
/tmp/ccWSFke9.o: In function `main':
gmptest.c:(.text+0x8): undefined reference to `__gmpz_init'
collect2: ld returned 1 exit status
make: *** [static] Error 1
该库确实存在:
chris@vostro:~/Dropbox/static$ ls -lA /usr/lib/libgmp.a
-rw-r--r-- 1 root root 1041666 2010-02-26 13:20 /usr/lib/libgmp.a
我也尝试过 -lgmp 进行静态链接,但错误是相同的。
这一切都在 Ubuntu 10.04 和 10.10 AMD64 上。
有人可以告诉我我犯的明显错误吗?
谢谢,
克里斯。
The GMP docs say that static linking may provide a small performance improvement.
I am having a problem getting it to staticly link libgmp on my Linux systems. I've narrowed down the issue I'm having to a tiny test case.
gmptest.c
#include <gmp.h>
int main(int argc, char** argv) {
mpz_t foo;
mpz_init(foo);
return 0;
}
Makefile:
all: clean gmptest static
clean:
rm -f *.s
rm -f *.o
rm -f gmptest
rm -f static-gmptest
gmptest: Makefile gmptest.c
gcc -std=c99 -O3 -lgmp gmptest.c -o gmptest
static: clean Makefile gmptest.c
gcc -std=c99 -O3 -static /usr/lib/libgmp.a gmptest.c -o static-gmptest
The non-static binary is compiled and linked without any issues, but 'Make static' produces:
gcc -std=c99 -O3 -static /usr/lib/libgmp.a gmptest.c -o static-gmptest
/tmp/ccWSFke9.o: In function `main':
gmptest.c:(.text+0x8): undefined reference to `__gmpz_init'
collect2: ld returned 1 exit status
make: *** [static] Error 1
The library does exist:
chris@vostro:~/Dropbox/static$ ls -lA /usr/lib/libgmp.a
-rw-r--r-- 1 root root 1041666 2010-02-26 13:20 /usr/lib/libgmp.a
I have also tried -lgmp for the static linking, but the error is the same.
This is all on Ubuntu 10.04 and 10.10 AMD64.
Can some enlighten me as to the obvious error I'm making?
Thanks,
Chris.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试
,因为库应该始终以良好的顺序链接,并且位于使用它们的程序或目标文件之后。
Try
since libraries should always be linked in the good order, and after the program or object files using them.
在这里,在 GMP 6.1.2 / MINGW 中并假设具有一定的可移植性,我发现标头“gmp.h”具有固定链接模式,如使用 GMP 构建参数配置的那样。
与此一样,编译器永远不会生成静态对象装饰,因此链接器永远不会匹配静态 libgmp,我限制了定义
__GMP_LIBGMP_DLL
现在,定义了
GMP_STATIC
静态 < code>libgmp.a 已成功吸引,并且没有GMP_STATIC
动态libgmp.dll.a
。Here, in GMP 6.1.2 / MINGW and assuming certain portability, I find that the header "gmp.h" has fix link mode, as configured with GMP build parameters.
As with this the compiler will never generate static object decorations, and so the linker will never match the static libgmp, I conditioned the define
__GMP_LIBGMP_DLL
Now, with defined
GMP_STATIC
the staticlibgmp.a
is successfully attracted and withoutGMP_STATIC
the dynamiclibgmp.dll.a
.