Search code examples
compilationstatic-linking

What's the difference between linking directly and linking as a static libarary


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?


Solution

  • 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.