0

We have an application where the code is organized in several libraries, some of which depend on other of those libraries, so we have a dependency tree like

App
 |
 +--------+--------+
 |        |        |
 v        v        v
lib1     lib2     lib3
 |        |
 v        v
lib3     lib3

Recently somebody added a new method in lib3, which depends on a class defined in lib2 and, since that was generating circular includes, we made a forward declaration of the class needed in the header file present in lib3.

Now, each library is compiled to a static library and then linked against its respectively linking list, so lib2 is in the linking listo of lib2 and also lib3 is in the liking list of lib2.

This works perfectly so far, but I was wondering the drawbacks related to have that kind of compile and linking dependencies. I thought that might happen that a change in lib2 won't be noticed by lib3 unless it is recompiled, but I checked and any change in any header file in lib2 would trigger the recompilation of lib3 (a little luck here).

Is there any other important drawback I should be aware of??

Rodrigo
  • 231
  • 3
  • 16
  • *"...any change in any header file in lib2 would trigger the recompilation of lib3."* Why? Is that something you imposed in the build system, or did it arise naturally due to the pattern of `#include` statements? And if the latter, are you sure all those `#include` statements are really necessary? – Beta May 30 '16 at 18:56
  • One method I have used in this case is to define a base class with a pure virtual function in one library. Derive from it in the other library. Then you reduce the dependency. Maybe there are other methods. I think you should avoid the circular dependency. – user2672165 May 30 '16 at 19:19
  • With a circular dependency you always have to link with all libraries. Then you could as well create one big static library file. – user2672165 May 30 '16 at 19:25
  • @Beta that's not an imposed condition, it's more like a lucky condition, but we would like to remove the luck from the panorama. – Rodrigo May 31 '16 at 18:32

1 Answers1

2

Is there any other important drawback I should be aware of??

Well, the order of libraries specified for linking actually matters.

To get rid of the order which the linker uses to resolve these dependencies, there usually is an option provided to group them, like these were just using a single pool of .obj/.o files.

For the GCC compiler these options are -Wl,--start-group,-Wl,--end-group.

Community
  • 1
  • 1
πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175