如何使用 pkg-config 静态链接库 [英] How to use pkg-config to link a library statically

查看:99
本文介绍了如何使用 pkg-config 静态链接库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想静态链接 pkg-config 找到的 libpng.

I'd like to link libpng found by pkg-config statically.

pkg-config --libs --static libpng

输出

-L/usr/local/Cellar/libpng/1.6.15/lib -lpng16 -lz

-L/usr/local/Cellar/libpng/1.6.15/lib -lpng16 -lz

我在那个目录中有两个 libpng16.a libpng16.dylib,如果我使用这些标志,库会动态链接.

I have both libpng16.a libpng16.dylib in that directory, and if I use these flags the library gets linked dynamically.

如何告诉 pkg-config 或链接器(最好以某种可移植的方式)我真的希望它静态链接?

How can I tell either pkg-config or the linker (preferably in some portable-ish way) that I really want it linked statically?

我已经尝试在 pkg-config 的标志之前添加 -static,但这使得 clang 的 ld 尝试链接crt0.o"但失败.

I've tried adding -static before pkg-config's flags, but that makes clang's ld try and fail to link "crt0.o".

推荐答案

pkg-config --static 选项依赖于 .pc 文件中的正确标记.如果提供 --static 选项没有返回链接 libpng 存档所需的正确信息,那么您不能为此目的使用 pkg-config.

The pkg-config --static option relies on proper tagging in the .pc files. If providing the --static option does not return correct information necessary to link against the libpng archive, then you cannot use pkg-config for that purpose.

我怀疑 libpng(以及大多数其他软件包)在 libpng 1.2 之后的一段时间内放弃了对静态链接的支持.他们可能仍然提供库存档,但 libpng pkg-config 文件不再正确标记以支持静态链接.您必须手动告诉 ld 使用静态库.

I suspect libpng (along with a majority of other packages) dropped support for static linking some time after libpng 1.2. They may still provide a library archive, but the libpng pkg-config file is no longer properly tagged to support a static link. You will have to manually tell ld to use the static lib.

这篇关于如何使用 pkg-config 静态链接库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆