C ++:如何创建两个相互依赖的共享库? [英] C++: How to create two interdependent shared libraries?

查看:273
本文介绍了C ++:如何创建两个相互依赖的共享库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要将a.cpp和b.cpp编译为两个单独的共享库liba.so和libb.so,并且这两个库是相互依赖的,我该怎么做呢?

Suppose I want to compile a.cpp and b.cpp into two separate shared libraries liba.so and libb.so and the two libraries are interdependent, how can I go about doing it?

推荐答案

虽然我可以想到几种类似的方法,但它们都有各自的缺点,并且由于以下原因,整个问题尚无定论

Although I can think of several hack-ish way to implement something like that, they all have their own disadvantages, and the whole issue is moot for the following reason.

尽管您没有明确提及使用的平台,并且共享库实现的详细信息是特定于平台的,所以无论如何,对此的答案一个问题取决于平台-给定您在问题中提到的共享库的名称 liba.so和 libb.so,您最有可能使用Linux。

Although you did not explicitly mention which platform you use -- and details of shared library implementation is highly platform specific, so that, in any case, the answer to this kind of a question is platform dependent -- given the names of the shared libraries that you mention in your question, "liba.so", and "libb.so", you are most likely using Linux.

大多数当前的Linux发行版都配置了它们的运行时加载程序,因此应用程序只能从应用程序显式链接到的共享库中解析外部引用。如果您的应用程序与liba.so链接,而liba.so与libb.so链接,则您的应用程序将无法解析libb.so中的外部引用,反之亦然。

Most current Linux distributions have configured their runtime loader so that the application can resolve external references only from shared libraries that the application explicitly links with. If your application links with liba.so, which links with libb.so, your application will not be able to resolve external references from libb.so, and vice versa.

因此,由于您的应用程序将明确需要链接到两个共享库,因此整个问题都没有解决。您最好分别构建两个共享库,然后将应用程序与两个共享库明确链接。否则,它将无法正常工作。

Therefore, since your application will explicitly need to link with both shared libraries, this whole issue is moot. You might as well build both shared libraries individually, and explicitly link your applications with both of them. Otherwise, it won't work.

但是,如果您真的坚持要破解它,那么将每个库彼此明确地链接起来就不那么复杂了。有一个初始引导问题,可以通过虚拟的Makefile目标来解决。简而言之, Makefile 可能看起来像这样:

But if you really insist on hacking this up, this shouldn't be any more complicated then explicitly linking each library with the other. There's a matter of initial bootstrapping, which can be solved via dummy Makefile targets. Briefly, the Makefile would probably read something like this:

liba.so: liba.so-target libb.so-target
    [link command with -llibb.so]

libb.so: libb.so-target liba.so-target
    [link command with -lliba.so]

liba.so-target: [all object modules that build liba.so]
    [link command without -llibb.so]
    touch liba.so-target

libb.so-target: [all object modules that build libb.so]
    [link command without -lliba.so]
    touch libb.so-target

因此,最终,每个共享库都首先获得自己链接,然后重新链接到另一个库,另一个库的依赖也重新链接,这也要归功于它的依赖。

So, at the end of the day, each one of the shared libraries first gets linked by itself, then relinked against the other library, with the other library's dependency also getting relinked, thanks to its dependency too.

这不是100%完美的,这这种方法有时可能会遇到并发并行make构建的问题,但这是总的想法。我仍然建议,考虑到Linux运行时加载程序的当前配置,您的应用程序必须与两个库显式链接,从而使整个事情完全没有必要。

This isn't 100% perfect, this approach might experience occasional issues with concurrent, parallel make builds, but this is the general idea. I still suggest that given the current configuration of Linux's runtime loader, your application will have to be explicitly linked with both libraries, making this whole thing completely unnecessary.

这篇关于C ++:如何创建两个相互依赖的共享库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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