You are not logged in.

#1 2021-12-10 15:38:16

dragomang87
Member
Registered: 2014-09-19
Posts: 3

g++ -l order irrelevant, ld --as-needed disabled?

The man page of g++ says regarding the -l option:

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified.  Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o.  If bar.o refers to functions in z, those functions may not be loaded.

This seems to be a consequence of the ld option --as-needed. From the ld man page:

This option affects ELF DT_NEEDED tags for dynamic libraries mentioned on the command line after the --as-needed option.  Normally the linker will add a DT_NEEDED tag for each dynamic library mentioned on the command line, regardless of whether the library is actually needed or not.  --as-needed causes a DT_NEEDED tag to only be emitted for a library that at that point in the link satisfies a non-weak undefined symbol reference from a regular object file or, if the library is not found in the DT_NEEDED lists of other needed libraries, a non-weak undefined symbol reference from another needed dynamic library.  Object files or libraries appearing on the command line after the library in question do not affect whether the library is seen as needed.  This is similar to the rules for extraction of object files from archives.  --no-as-needed restores the default behaviour.

The --as-needed option seems to be the default for many years now.
However I was compiling the examples from Givaro (https://github.com/linbox-team/givaro), e.g examples/FiniteFields/all_field.C, using the installed library (givaro arch package) and it seems that g++ on Arch does not care about the order of the libraries. Namely both

g++ -lgivaro -lgmp -lgmpxx all_field.C -o tmp.bin

and

g++  all_field.C -o tmp.bin -lgivaro -lgmp -lgmpxx

work fine on Arch (Linux hostname 5.12.15-arch1-1 #1 SMP PREEMPT Wed, 07 Jul 2021 23:35:29 +0000 x86_64 GNU/Linux). The libraries to use (-lgivaro -lgmp -lgmpxx, and the folders) are obtained by

givaro-config --prefix --libs --cflags

The first command

g++ -lgivaro -lgmp -lgmpxx all_field.C -o tmp.bin

fails to compile on Ubuntu (many undefined references from ld) and the same failure is obtained on Arch by passing the option to ld explicitly with

g++ -Wl,--as-needed -lgivaro -lgmp -lgmpxx all_field.C -o tmp.bin

So it seems that Arch does not use --as-needed as default, but it is part of the defaults in makepkg.config and it seems to be the default behavior from upstream. Does Arch changes the default? if yes, where? if not, how it this behavior determined?

Offline

#2 2021-12-10 23:18:56

mpan
Member
Registered: 2012-08-01
Posts: 750
Website

Re: g++ -l order irrelevant, ld --as-needed disabled?

The order is important if an -l option evaluates to an archive (.a), as they are scanned only once during linking. So the linker must already know, what objects to extract from archive, when it encounters the -l option. The -as-needed is related to dynamic libraries (.so).


Sometimes I seem a bit harsh — don’t get offended too easily! PGP: 7C848198AE93D3BB

Offline

#3 2021-12-11 10:06:25

dragomang87
Member
Registered: 2014-09-19
Posts: 3

Re: g++ -l order irrelevant, ld --as-needed disabled?

The error messages are from the linker ld, so that's why I am guessing the --as-needed option is disabled silently. The -Wl, option passes arguments to the the linker.

Last edited by dragomang87 (2021-12-11 11:41:37)

Offline

Board footer

Powered by FluxBB