1
votes

I have some Questions on linking libraries.

How does the Linker decide, if a library I want to link is linked static or dynamic? Is it decided by the file extention (.a/.so)?

Is it possible, to dynamically link a .a library?

Is it possible to convert a .a library into a .so library without having the sources?

2
Yes, no, no. You generally need to compile code position-independently to make it useable as a shared library; this is not usually done unless explicitly requested. - Kerrek SB
@KerrekSB Shouldn't that be an answer? - user395760

2 Answers

1
votes
  1. The linker decides how to link the library by generally looking at the extension, but that's pretty much up to the linker. The AIX linker has some rather exotic behaviour. Moreover, if you have both a .so and a .so version of the library in the same place, the command line switches you have given the linker will determine which one it uses
  2. Sort of. You can link a .a into a .so but there will be performance issues - shared libraries should be built with position independent code for best performance. And depending on the code, the linker might refuse to link it because it can't patch up the relocation information. But you can't tell the linker to treat a .a as a .so
  3. As above - maybe.
0
votes

The gcc linker will link dynamically to .so files by default, if both types of library are found in its search path. You can over-ride this with command line arguments, as described here.