有没有办法静态链接 glib2 库?

发布于 2025-01-20 11:36:36 字数 805 浏览 2 评论 0原文

由于生产设备没有glib2运行环境,所以我需要静态链接到glib2。我尝试通过以下方式编译和链接,但仍然链接libglib-2.0.so.0。我确信我的设备上存在 libglib-2.0.a 。

#include <glib.h>

int main(void)
{
    char *test;
    test = g_new0(char,1);
    g_free(test);

    return 0;
}

我尝试通过以下方式构建它

gcc `pkg-config --static --libs --cflags glib-2.0` main.c -o test
ldd test
linux-vdso.so.1 (0x00007ffe9dbc8000)
        libglib-2.0.so.0 => /lib64/libglib-2.0.so.0 (0x00007f2ee333f000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f2ee31fb000)
        libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f2ee3183000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f2ee3162000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f2ee2f93000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f2ee3492000)

Since the production device does not have a glib2 runtime environment, I need to statically link to glib2. I tried to compile and link in the following ways, but it still links libglib-2.0.so.0. I am sure that libglib-2.0.a exists on my device.

#include <glib.h>

int main(void)
{
    char *test;
    test = g_new0(char,1);
    g_free(test);

    return 0;
}

i tried build it by

gcc `pkg-config --static --libs --cflags glib-2.0` main.c -o test
ldd test
linux-vdso.so.1 (0x00007ffe9dbc8000)
        libglib-2.0.so.0 => /lib64/libglib-2.0.so.0 (0x00007f2ee333f000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f2ee31fb000)
        libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f2ee3183000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f2ee3162000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f2ee2f93000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f2ee3492000)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

后eg是否自 2025-01-27 11:36:36

pkg-config - static的标志为您提供了适合最终静态链接的程序的库标志。现在,它不是pkg-config配置您的构建的作业,只是为了给您库所需的标志。

因此,您需要将-s添加到gcc命令自己。

The flags that pkg-config --static gives you are library flags suited for a program that will ultimately be statically linked. Now, it's not pkg-config's job to configure your build, just to give you the flags your libraries need.

So, you need to add -s to your gcc command yourself.

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