4

How do I link a shared library with --as-needed using Automake? When I tried adding the flag to LDFLAGS, I saw libtool called as so:

/bin/bash ../../libtool --mode=link ... -Wl,--as-needed ... dependencies

Which results in a call to GCC like so:

gcc -shared ... dependencies ... -Wl,--as-needed ...

But that's the wrong order.

  • Is there a way to do this with Libtool?

  • Or is there a nice way to build shared libraries using Automake but without Libtool? (I've been frustrated at Libtool for various other reasons in the past...)

It seems the Debian folks ran into this problem too (bug report) but I'd like to be able to fix this for my project rather than messing with my system (unless I misunderstand the fix).

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • I was hoping that things had changed, given that your reference was from 2006 and mentioned `libtool` 1.5, but it seems not: http://sigquit.wordpress.com/2011/02/16/why-asneeded-doesnt-work-as-expected-for-your-libraries-on-your-autotools-project/ – Jack Kelly Jul 28 '11 at 22:51
  • 1
    Yeah, I'm running libtool 2.2.6. – Dietrich Epp Jul 29 '11 at 00:02

3 Answers3

5

You can fix that just for your project by modifying the ltmain.sh script in your project sources. You can even add it as part of autotools bootstrapping, as in: https://meego.gitorious.org/tracker/tracker/commit/cf2ca3414aeba146dceacc5ecd84765f4c08a06f

Aleksander
  • 66
  • 1
2

You can fix it in Makefile.am; basically it's the same as the answer to my question here, except you need to use -(no-)as-needed instead of -(no-)whole-archive.

Community
  • 1
  • 1
ptomato
  • 56,175
  • 13
  • 112
  • 165
  • Could you expand on this? The problem is that putting it in LDFLAGS doesn't work. – Dietrich Epp Jul 31 '11 at 19:36
  • Well, it seemed to me the problem was that it's put into the command line in the wrong order. By bracketing the shared library name on the command line with options turning the behavior on and off, it seems to me it should work in LDFLAGS. Or have I misunderstood completely? – ptomato Jul 31 '11 at 19:38
  • 1
    The problem is that when I pass the flags to libtool in the correct order, libtool reorders them. – Dietrich Epp Jul 31 '11 at 19:39
  • But this solution puts the `as-needed` flags and the dependencies in one non-reorderable block, which libtool sees as one command-line argument. – ptomato Jul 31 '11 at 19:42
  • Okay, but I need to construct the command line using variables from a big nasty variable coming from Autoconf, which contains other `-Wl` flags in it. – Dietrich Epp Jul 31 '11 at 19:48
  • Hmm, then it probably depends on the specifics whether this solution will work or not. – ptomato Jul 31 '11 at 20:25
0

Really a frustrating issue, especially for package maintennance.

Debian has built in support to patch libtool and fix this problem in debian packages

dh_autoreconf --as-needed

Or, if you're using CDBS:

include /usr/share/cdbs/1/rules/autoreconf.mk

DEB_DH_AUTORECONF_ARGS += --as-needed
Ichthyo
  • 8,038
  • 2
  • 26
  • 32