1

As known, when linking target to a series of libraries using gcc, we need to give the compiler (gcc) the correct order of the linked libraries. For example:

Source file a.cpp depends on libb.a, libc.a and libd.a.

Library libb.a depends on libc.a and libd.a.

Therefore, the compiling command should be

g++ a.cpp -o a libb.a libc.a libd.a

Moreover, we cannot reverse the order between b and c or between b and d. Normally, we have to manually figure out the order of all linked libraries. Certainly, there is also some way to bypass this problem. We can add

-Wl,--start-group $(LIBRARIES_YOU_NEED) -Wl,--end-group 

so ld can figure out the order by itself. However, I would like to know the order of the linked libraries for myself.

So, given all libraries needed by a source file, are there any automatic ways to sort the libraries so that the dependency between them are correct when they are put in the gcc command line?

user3677630
  • 635
  • 6
  • 14
  • 1
    Consider learning [pkg-config](https://www.freedesktop.org/wiki/Software/pkg-config/); in general there is no automatic ways to sort libraries; in pathological cases you could have circularities; and you should prefer shared libraries – Basile Starynkevitch Sep 21 '17 at 06:48
  • 1
    "so ld can figure out the order by itself". It can't. It just keeps cyclically examinining the grouped libraries until no new undefined references are generated. Brute force. – Mike Kinghan Sep 21 '17 at 19:14

0 Answers0