1

Imagine we have an executable exe that depends on shared library foo, which in turn depends on shared library bar: exe needs libfoo.so and foo needs libbar.so. Then, bar is changed in a source-compatible manner (i.e. API unchanged), but not in a binary-compatible manner (i.e. ABI changed). We must therefore relink (or face segmentation faults).

The question is: What exactly should be recompiled/relinked?


Let me make that a little more specific. To link exe, we need not add the compiler option "-lbar", whereas we do need that option for linking foo:

gcc -fPIC -shared -I. -Ibar -lbar -o libfoo.so foo.o
gcc -fPIC -I. -Ifoo -Ibar -lfoo -o exe exe.o
  1. Therefore, is it sufficient to relink foo, and leave exe as is?

  2. Do foo and exe also need recompilation (if bar is changed, but its API was unchanged)?

Lastly, I have tried leaving foo unchanged, but relinked exe with the added "-lbar" compiler option:

gcc -fPIC -I. -Ifoo -Ibar -lfoo -lbar -o exe exe.o

This did eliminate the segmentation fault, which could imply that everything is then correctly linked... or it could by shear luck (because segmentation faults do not always occur, even though there is an inherit memory problem).

  1. Is it allowed to do that, i.e. leave foo unchanged, but explicitly link exe against bar?

Somewhat related readings: [1][2][3][4][5]

  • This is the kind of grunt work you should always leave up to a machine to figure out. Not because it is hard to do, but because it is tedious and you'll lose an hour of your life when you don't pay attention enough. Lots and lots of build systems out there, pick one. – Hans Passant Jun 21 '17 at 14:01
  • @HansPassant I am using a build system, [wmake](https://cfd.direct/openfoam/user-guide/compiling-applications/), which is custom to the open-source Computational Fluid Dynamics software called OpenFoam. The reason for asking is that OpenFoam consists of many different executables ("solvers") which rely on many dynamic libraries to do the work. I adapted one such library (_bar_) for my purposes, and I had figured that by simply overwriting the existing dynamic library all solvers would simply work: but they gave me segfaults. That's why I am asking _what exactly_ should be relinked. – Kevin van As Jun 21 '17 at 14:33

1 Answers1

0

Until an expert answers, these are my own hypotheses:

  1. Given that the linker does not require "-lbar" for exe, I'm inclined to conclude that exe does not need re-linking as the linker apparently does not need bar. However, I am very unsure about this, as this assumes that the linker does not find bar via foo. (After all, ldd libfoo.so will show me bar, so I'd say that the linker may possess the knowledge that it needs bar.)

  2. No, they do not. The sources are compatible, so the symbols inside the compiled (object) files should still be correct/compatible. The executable does, however, need relinking, because the location of those symbols inside the object files has changed.

  3. It appears that that is allowed... But I'm not sure. Also: any executable/library that depends on foo other than exe will still face segmentation faults, so this would be bad-practice, would it not?