[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How to use --as-needed

On Wed, Nov 17, 2010 at 03:12:35PM +0000, Tony Houghton wrote:
> On 17/11/10 13:47, Roger Leigh wrote:
>> On Wed, Nov 17, 2010 at 01:34:21PM +0000, Tony Houghton wrote:
>>> How should one add --as-needed linker flags to an autotools-based
>>> package? I ended up adding CFLAGS=-Wl,--as-needed to my
>>> dh_auto_configure parameters but is it OK to just overwrite CFLAGS like
>>> that, or should I add it to any existing CFLAGS?
>>> BTW, at first I tried LDFLAGS=--as-needed but automake/libtools passed
>>> it to gcc without -Wl, which I thought wasn't very clever. I then used
>>> CFLAGS because the -Wl makes it a gcc option, but OTOH it is explicitly
>>> a linker option (according to the man page) so perhaps I should stick
>>> with LDFLAGS after all?
>> Check the rule in your generated Makefile.in (example):
>>          --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \
>>          $(LDFLAGS) -o $@
>> You can see LDFLAGS is used, but --as-needed (and --no-as-needed)
>> are /positional/ parameters.  So you need --as-needed immediately
>> before $(AM_LDFLAGS), which is why you see it working when added
>> to $(CFLAGS).  I would just add it to the front of AM_LDFLAGS.
> AIUI AM_LDFLAGS is only meant to be used in Makefile.am (and not
> guaranteed to be used anyway, if there are per-program flags) and you
> should use LDFLAGS to pass in flags from an external command or script
> such as debian/rules. Does it matter if --as-needed comes after other
> flags as long as it's before the list of objects to link?

Yes.  You could, for example, have
  -Wl,--as-needed -lfoo -lbar -Wl,--no-as-needed -lbaz
which would only apply it to libfoo and libbar.

Where you want to do this depends upon what level you want to work
at.  You could patch the Makefile.am to to exactly what you want,
and here you're free to tamper with AM_LDFLAGS etc, or even introduce
entirely new variables which may be set/overridden later.  If you're
just passing flags in debian/rules then you're more limited.

>> BTW, it's worth considering whether or not you actually need to use
>> --as-needed, or if there are better ways of removing unwanted
>> libraries (such as not linking them in in the first place).
> I think it's caused by pkg-config adding libraries that my binaries
> don't depend on directly, but do depend on via the higher level
> libraries they directly link with. Looking at the package dependencies
> (for roxterm) there's nothing there that I'm not fairly certain is
> needed so perhaps it would be best just to ignore the warnings?

They are certainly harmless.  Ideally, the dependency problem should
be fixed on the pkg-config side by trimming what gets linked in by the
GTK+ .pc files etc.

It might be worth compiling with GCC 4.5 from experimental and also
linking with the gold linker in place of the standard ld.  These have
stricter linking requirements, and it may be the case that with gold
those extra libs may be needed.

(changes planned for wheezy, currently being discussed on -ports and
-gcc; I don't agree with it all, mainly making --add-needed the default,
but it's useful to know what's coming up after squeeze.)


  .''`.  Roger Leigh
 : :' :  Debian GNU/Linux             http://people.debian.org/~rleigh/
 `. `'   Printing on GNU/Linux?       http://gutenprint.sourceforge.net/
   `-    GPG Public Key: 0x25BFB848   Please GPG sign your mail.

Attachment: signature.asc
Description: Digital signature

Reply to: