UNIX:将可执行文件链接到静态库和动态库?
我承认这是一个作业问题,询问是否可以。我相信答案是肯定的,但是我不知道“为什么?”问题的答案。对我来说,答案似乎只是“为什么不呢?” ...有人可以更深入了解为什么这是真的(还是不真实,如果我错了)?似乎只会问这个问题是否有一个棘手的部分,但是据我了解,似乎没有任何可以禁止与两者链接的东西。
I admit this is a homework question that asks if this is possible. I believe the answer to it is yes, however I do not know the answer to the question 'why?'. To me, it seems like the answer is simply 'why not?'... can anyone provide some deeper insight to why this is true (or untrue, if I am wrong)? It seems like this question would only be asked if there was a tricky part to it, but from what I understand of compiling, there doesn't seem to be anything that would disallow linking to both.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,当然您可以链接静态库和动态库。链接到静态库本质上与从存档中获取所需的目标文件并将它们包含在链接过程中相同。
如果您链接的静态库和动态库是相同库,则动态链接是多余的。它仍然是允许完成的,但它是毫无意义的(并且只会为你的可执行文件引入不必要的运行时依赖,至少在 Unix 上)。
Yes, of course you can link both a static library and a dynamic library. Linking to a static library is essentially the same as grabbing the required object files from the archive and including them in the linking process.
If the static library and the dynamic library you're linking is for the same library, then the dynamic link is redundant. It's still allowed to be done, but it'd be pointless (and just introduces an unnecessary runtime dependency for your executable, at least on Unix).
通常,应该没有理由不能链接到静态和动态库,尤其是当它们不冲突时(例如,提供相同的符号)。这样做的机制将根据您的合格器而有所不同,但是许多免费软件项目实际上是这样做的 - 它们在静态上构建了所谓的“便利库”,然后将其链接到动态链接的可执行文件中。
In general, there should be no reason why you can't link to both static and dynamic libraries, especially if they do not conflict (for instance, provide the same symbols). The mechanism to do so will differ depending on your complier, but many Free Software projects actually do this -- they build a so-called "convenience library" statically that then gets linked into a dynamically linked executable.