C++: How to create two interdependent shared libra


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.

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.

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.

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.

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.


  • Including commands from separate Ruby file
  • How to create list with carousel effect in android
  • Emulate User Activity
  • Git subtree post hook
  • How many percent of the tweets does twitter sample API give?
  • How to create a data template dependent on an XML Attribute?
  • How do I recognize a line break with a switch case that evaluates a char in Java?
  • Portable JRE on Linux - possible?
  • How can I get the choice “H2” back in the H2 consol?
  • NHibernate manually control fetching
  • abstracting over a collection
  • Lock Horizontal View
  • Jquery Knockout: ko.computed() vs classic function?
  • Can't remove headers after they are sent
  • C++ pointer value changes with static_cast
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • Installed module is empty
  • Yii2: Config params vs. const/define
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • Master page gives error
  • Spring security and special characters
  • Excel - Autoshape get it's name from cell (value)
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Arrays break string types in Julia
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Matrix multiplication with MKL
  • PHP: When would you need the self:: keyword?
  • KeystoneJS: Relationships in Admin UI not updating
  • Acquiring multiple attributes from .xml file in c#
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • coudnt use logback because of log4j
  • reshape alternating columns in less time and using less memory
  • Unable to use reactive element in my shiny app
  • Converting MP3 duration time
  • How can I use threading to 'tick' a timer to be accessed by other threads?