在 Linux 上静态链接 libgmp 时出错

发布于 2024-12-14 11:56:20 字数 1182 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为一人 2024-12-21 11:56:20

请尝试

 gcc -std=c99 -O3 -static gmptest.c -lgmp -o static-gmptest

,因为库应该始终以良好的顺序链接,并且位于使用它们的程序或目标文件之后。

Try

 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.

花开半夏魅人心 2024-12-21 11:56:20

在这里,在 GMP 6.1.2 / MINGW 中并假设具有一定的可移植性,我发现标头“gmp.h”具有固定链接模式,如使用 GMP 构建参数配置的那样。

/* Instantiated by configure. */
#if ! defined (__GMP_WITHIN_CONFIGURE)
    #define _LONG_LONG_LIMB 1
    #define __GMP_LIBGMP_DLL  0
#endif

与此一样,编译器永远不会生成静态对象装饰,因此链接器永远不会匹配静态 libgmp,我限制了定义 __GMP_LIBGMP_DLL

/* Added link switch GMP_STATIC */
#if ! defined (__GMP_WITHIN_CONFIGURE)
    #define _LONG_LONG_LIMB 1
    #ifndef GMP_STATIC // SGR 2021-12-30
        #define __GMP_LIBGMP_DLL  1
    #endif
#endif

现在,定义了 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.

/* Instantiated by configure. */
#if ! defined (__GMP_WITHIN_CONFIGURE)
    #define _LONG_LONG_LIMB 1
    #define __GMP_LIBGMP_DLL  0
#endif

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

/* Added link switch GMP_STATIC */
#if ! defined (__GMP_WITHIN_CONFIGURE)
    #define _LONG_LONG_LIMB 1
    #ifndef GMP_STATIC // SGR 2021-12-30
        #define __GMP_LIBGMP_DLL  1
    #endif
#endif

Now, with defined GMP_STATIC the static libgmp.a is successfully attracted and without GMP_STATIC the dynamic libgmp.dll.a.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文