Pages

Feb 11, 2014

Going dynamic

The Fortran people seem to really like the static libraries, why I couldn't really fathom. The idea behind a shared object (or dynamic library in Windows) is quite simple — flexibility. Their explanation is that static libraries (basically an object file) provides all that you need for linking and doesn't cause the problems loaders do. This is not quite true and there are some problems though:
  • If you link against a shared binary in the static library code, the linker has no way of incorporating it, thus creating references exactly like these you have in a dynamic library.
  • When you want to change something in a statically linked code, you have to rebuild the whole thing.
  • (Especially in Linux/Unix) The code is really shared(!), meaning a lot of applications use the same binary, changes in that binary propagate to all and additionally there is only one copy of each one binary unit.
Now dynamic linking is not without cost:
  • A person should ensure binary compatibility of the library as to be able to change the implementation without rebuilding the user applications. This could be quite a bit of work, especially for people who don't have a good set of habits for doing it.
  • You should ensure all the dependencies are in a place known to the loader.
  • Resolving of symbols may be done by the runtime as it is in Linux/UNIX.
  • The library interface and exports should be enforced, as in Linux/UNIX by default all symbols are exported, while in Windows none of them are.
All in all dynamic libraries require a bit more consistent and rigid design, while static libraries could be less fuss. In the long rung though, the advantages of dynamic linking are overwhelming and it is the better way, allowing you to be flexible about your code.