1

I am learning about linking and static libraries. In one exercise (from Computer Systems, Bryant - O'Hallaron), I am asked to write the minimal command line that will allow the static linker to resolve all symbol references. Consider

p.o depends on libx.a
libx.a depends on liby.a
liby.a depends on libx.a
libx.a depends on p.o

I wrote:

gcc p.o libx.a liby.a libx.a p.o

but the correct answer is

gcc p.o libx.a liby.a libx.a

Why should the extra p.o not figure in the command line?

bigTree
  • 2,103
  • 6
  • 29
  • 45
  • 1
    This might be helpful: http://stackoverflow.com/questions/45135/linker-order-gcc – Vinbot Jul 21 '14 at 17:20
  • You are probably confuzzled by the behavior of the compiler, it makes a single pass through the source code so should see declarations before they are being used. Not an issue with the linker, it makes two passes. So doesn't have a problem resolving forward references. – Hans Passant Jul 21 '14 at 17:24

1 Answers1

1

You cannot link the same object twice, otherwise you'd get duplicate references. The dependency is resolved by the linker. If the reference is found, the linker continues. That's why you don't need to indicate p.o twice.

jweyrich
  • 31,198
  • 5
  • 66
  • 97