I tried to statically link some built libraries using the static option in the linker. I'm using SCons to compile/link the libraries. I was under the impression that static builds happen at link time, and therefore it did not matter what you were linking. I got a bunch of errors and was wondering if there is any difference between .o files that were destined to be statically linked to the libraries and .o files that were destined to be dynamically linked to the libraries. Theoretically, should these be the same?
2 Answers
In the general case you can't use static and dynamic libraries interchangeably. See: Static link of shared library function in gcc
Regarding your actual question:
... if there is any difference between .o files that were destined to be statically linked to the libraries and .o files that were destined to be dynamically linked to the libraries. Theoretically, should these be the same?
No, they should not be the same. There are differences, the most notable one probably being that a dynamic library has a non-empty .dynsym
section.
If a function foo()
is linked statically into an application, then the functions defintion is going to be at a fixed (static!) location relative to the rest of the applications code.
On the other hand, if we link foo()
dynamically into our application, then at compile-time the application can't know where it can find the definition of foo()
during execution, since the application can't make any assumptions about the library - not about the librarys location at runtime nor about its internal structure. Therefore, the library itself provides a section .dynsym
in order for the client-code to be able to find foo
s definition although it wasn't clear at complie-time where this was going to be.

- 1
- 1

- 2,077
- 2
- 21
- 31
I don't know much about SCons, but as far as I know it's similar to other make systems and still does the same compiling/linking steps. The architecture of linking is already well documented on Stack Overflow, so I'll try to explain the differences.
Since static libraries are meant to resolve symbols into the executable at linking, they don't need anything special since resolving happens at compile time. Dynamic libraries need special consideration, since they are resolved at run time and the linker doesn't know how they're going to be used. You'll need special flags (e.g. the fPIC flag to make code that can be run anywhere in memory).
So the short answer is, yes, there are differences. To the best of my knowledge, an object file produced for dynamic linking can be turned into a static library (with some possible code bloat), but most object files made for static linking can't be used for dynamic linking since they probably don't have the necessary flags. I usually think of it (informally) as a static library being like another object file, while a dynamic library is like a standalone executable that is just hooked into (which is actually closer to the ELF format commonly used for shared libraries).

- 1
- 1

- 643
- 3
- 13