84

What is the purpose of those command line options? Please help to decipher the meaning of the following command line:

-Wl,--start-group -lmy_lib -lyour_lib -lhis_lib -Wl,--end-group -ltheir_lib

Apparently it has something to do with linking, but the GNU manual is quiet what exactly grouping means.

jww
  • 83,594
  • 69
  • 338
  • 732
pic11
  • 12,658
  • 17
  • 74
  • 108

1 Answers1

97

It is for resolving circular dependences between several libraries (listed between -( and -)).

Citing Why does the order in which libraries are linked sometimes cause errors in GCC? or man ld http://linux.die.net/man/1/ld

-( archives -) or --start-group archives --end-group

The archives should be a list of archive files. They may be either explicit file names, or -l options.

The specified archives are searched repeatedly until no new undefined references are created. Normally, an archive is searched only once in the order that it is specified on the command line. If a symbol in that archive is needed to resolve an undefined symbol referred to by an object in an archive that appears later on the command line, the linker would not be able to resolve that reference. By grouping the archives, they all be searched repeatedly until all possible references are resolved.

Using this option has a significant performance cost. It is best to use it only when there are unavoidable circular references between two or more archives.

So, libraries inside the group can be searched for new symbols several time, and you need no ugly constructs like -llib1 -llib2 -llib1

PS archive means basically a static library (*.a files)

Community
  • 1
  • 1
osgx
  • 80,853
  • 42
  • 303
  • 470
  • 1
    Accepted. A remark: I believe that GCC goes for dynamic libraries first, unless full file name (including path and .a suffix) is passed in the command line. -llib1 would cause GCC try to link to %.so file first and then try %.a file. – pic11 Apr 13 '11 at 16:06
  • 1
    @pic11, thanks. Linking is done by ld, and you can see how libraries are searched with adding `-Wl,--verbose` option to gcc (it will pass `--verbose` to linker ld). E.g. for `-ltest` library: `attempt to open /lib/libtest.so failed \n attempt to open /lib/libtest.a failed \n attempt to open /usr/lib/libtest.so failed \n attempt to open /usr/lib/libtest.a failed \n `. Linker try to open `.so` first, but then it try to open `.a`. It is done in every directory in library search dirs. – osgx Apr 13 '11 at 16:12
  • "searched for new symbols several time"? I think searching twice is enough to resolve all symbols. That should *not* be a *significant* performance cost. – Jimm Chen May 07 '12 at 01:56
  • 10
    Sorry, I finally realized "searching twice" is not enough. – Jimm Chen May 10 '12 at 04:41