Linux – gcc link target files and libraries – what’s the difference?

gcc link target files and libraries – what’s the difference?… here is a solution to the problem.

gcc link target files and libraries – what’s the difference?

Suppose there is this command:

g++ main.o somefile.o -lc -o main

What is the difference between the linked target file somefile.o and the linked library libc.a?

Solution

Files that end with “.a” are archives. They essentially contain a set of “.o”. So, assuming that “libc.a” contains “c1.o”, “c2.o”, and “c3.o”, your command is essentially equivalent to unarchiving “libc.a” and then calling:

g++ main.o somefile.o c1.o c2.o c3.o -o main

Note that objects contained in “.a” are only included when needed, that is, if at least one of their symbols is referenced by another “.o”.

Related Problems and Solutions