在 Linux 上静态链接 libgmp 时出错 [英] Error when statically linking libgmp on Linux
问题描述
GMP 文档说静态链接可能会提供小的性能改进.
The GMP docs say that static linking may provide a small performance improvement.
我在让它静态链接我的 Linux 系统上的 libgmp 时遇到问题.我已将问题范围缩小到一个小测试用例.
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;
}
生成文件:
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
该库确实存在:
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 用于静态链接,但错误是一样的.
I have also tried -lgmp for the static linking, but the error is the same.
这一切都在 Ubuntu 10.04 和 10.10 AMD64 上.
This is all on Ubuntu 10.04 and 10.10 AMD64.
有人能告诉我我犯的明显错误吗?
Can some enlighten me as to the obvious error I'm making?
谢谢,
克里斯.
推荐答案
尝试
gcc -std=c99 -O3 -static gmptest.c -lgmp -o static-gmptest
因为库应该总是以良好的顺序链接,并且在程序或目标文件使用它们之后.
since libraries should always be linked in the good order, and after the program or object files using them.
这篇关于在 Linux 上静态链接 libgmp 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!