Are there any differences between
gcc a.c b.c -o b.out
and
gcc a.c -o a.o
ar rcs liba.a a.o
gcc b.c -la -o b.out
?
In what circumstances shall I choose one over the other?
Are there any differences between
gcc a.c b.c -o b.out
and
gcc a.c -o a.o
ar rcs liba.a a.o
gcc b.c -la -o b.out
?
In what circumstances shall I choose one over the other?
Quoting part of dmckee's answer:
Dynamic linking can reduce total resource consumption (if more than one process shares the same library (including the version in "the same", of course)). I believe this is the argument that drives it its presence in most environments. Here "resources" includes disk space, RAM, and cache space. Of course, if your dynamic linker is insufficiently flexible there is a risk of DLL hell.
Dynamic linking means that bug fixes and upgrades to libraries propagate to improve your product without requiring you to ship anything.
- Plugins always call for dynamic linking.
- Static linking, means that you can know the code will run in very limited environments (early in the boot process, or in rescue mode).
- Static linking can make binaries easier to distribute to diverse user environments (at the cost of sending a large and more resource hungry program).
- Static linking may allow slightly faster startup times, but this depends to some degree on both the size and complexity of your program and on the details of the OSs loading strategy.
Read the full answer for more specific info.
Creating library (variant 2) is good when you are going to re-use the compiled module more than once, and the projects are really big (it saves a bit of compile time). Otherwise, there's no difference.