What are the --start-group and --end-group command line options?
Asked Answered
O

1

106

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.

Orthohydrogen answered 13/4, 2011 at 15:37 Comment(0)
C
125

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)

Calumnious answered 13/4, 2011 at 15:39 Comment(4)
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.Orthohydrogen
@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.Calumnious
"searched for new symbols several time"? I think searching twice is enough to resolve all symbols. That should not be a significant performance cost.Sedan
Sorry, I finally realized "searching twice" is not enough.Sedan

© 2022 - 2024 — McMap. All rights reserved.