我如何告诉 ld 忽略丢失的库?
我想为一堆可执行文件定义一个带有隐式规则的 Makefile,其中一些需要链接到自定义库(我们称之为 libedich.a
)。
我的问题是,当 libedich.a 尚未构建时,我希望能够构建那些不需要 libedich.a 的可执行文件。如果我只是将 -ledich
添加到 LDLIBS
变量中,当 libedich.a
不存在时,我会收到错误:
/usr/bin/ld: cannot find -ledich
我该如何告诉ld当给定的库不存在时可以继续链接吗?
I'd like to define a Makefile with implicit rules for a bunch of executables, some of which require linking against a custom-built library (let's call it libedich.a
).
My problem is that I'd like to be able to build those executables that do not require libedich.a
when the latter hasn't been built yet. If I simply add -ledich
to the LDLIBS
variable, I get errors when libedich.a
doesn't exist:
/usr/bin/ld: cannot find -ledich
How do I tell ld that it's okay to continue linking when a given library doesn't exist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
常见的解决方案是创建一个虚拟存档,以便 GCC 找到它。由于可执行文件不需要库中的任何符号,因此不会出错。像这样,
或者更简单,
A common solution is to create a dummy archive so that GCC will find it. Since the executable doesn't need any symbols from the library, it won't error out. Like this,
or even simpler,
这是使用一个 LDLIBS 变量来保存所有库依赖项并为每个目标重复使用它的缺点,即使您知道某些目标只需要库的子集。您有多种选择:
最后一个选项是我的推荐;这需要更多的工作,但是消除 Makefile 中的错误依赖关系将使您能够构建(也许是大部分)目标,即使其中一个依赖关系已损坏。一种方便的方法是使用特定于目标的变量:
您可能还想了解
make --keep-going
(make -k
)This is the downside of using one LDLIBS variable to hold all of the library dependencies and re-using it for every target, even though you know some targets only need a subset of the libraries. You have several options:
The last option is my recommendation; it is more work, but eliminating the false dependencies in your Makefile will enable you to build (perhaps most of) your targets even if one of the dependencies is broken. One convenient way to do this is with target-specific variables:
You probably also want to be aware of
make --keep-going
(make -k
)