It is ok now to file bugs for packages failing with --as-needed. File the bug and provide all the ussual needed info (emerge --info and error message for example). And make it depend on bug# 129413

I was reading this article on osnews and wondered what one would gain from this and what the problems could when using it in gentoo?

How to set LDFLAGS
There are a couple of ways to set the LDFLAGS.

1: export LDFLAGS="-Wl,--as-needed"
2: Set it globally in /etc/make.conf by adding LDFLAGS="-Wl,--as-needed"
3: Setting them per package, see below section named: Per package {LD,C}FLAGS

It should be set as LDFLAGS="-Wl,--as-needed" not as the article says as CFLAG!!

The story

Quote:

Today I thought about how I can make my very own application to only link against those libraries that it really requires and not those it get provided by PKG-CONFIG.

Problem-case:

When we use pkgconfig within our programs which of course makes sense, we also see that other libraries from other packages are being processed to our *_LIBS variables. So we end up in linking a lot of libraries to our application which we really do not need and which also causes (so I assume) a lot of overhead. After thinking for a while I looked some programs up by using:

readelf -d /usr/local/bin/gnome-terminal |grep NEEDED | wc -l

And saw that it was requiring over 52 libraries. I then recompiled gnome-terminal with

export CFLAGS = "-Os -s -Wl,--as-needed"

Pay attention to the --as-needed and the resulting binary required just 21 libraries afterwards instead of the 52 before because it only linked those really required to the final executable. I continued this with vte and saw that it required 29 libraries initially but after the --as-needed flag it went down to 7 libraries.

Now imagine this for the entire GNOME desktop or the entire KDE desktop and also package management systems which will reduce dependency tracking a lot. The problem with this method is that you surely don't keep track of API compatibility but a good package management system will certainly make sure that the packages packed belong to a certain desktop version.

How to filter the flags in ebuilds
There is a way to have portage filter this flag at compile time:

New LDFLAG --enable-new_ldflags
There is another LDFLAG which is more safe then plain --as-needed. The flag is --enable-new_ldflags and devsk eplains it best

defsk wrote:

when passed enable-new_ldflags, configure just checks if --as-needed works and if it does then it just assigns the variable LDFLAGS_AS_NEEDED="-Wl,--as-needed", which Makefiles have the liberty of using. Currently most kde Makefile's do. If a component finds that it doesn't work for them, they don't put it on the specific link line. Its manual in that sense.

one more thing is configure arg is --enable-new_ldflags and NOT --enable-new-ldflags as mentioned by the ./configure --help.

Per package {LD,C}FLAGS
There is a way to have a per package ldflag. It involves copying an bashrc file into /etc/portage and creating a file package.ldflags also in /etc/portage. Get the script here and full explanation here. Thanks to thebigslide for creating the original bashrc and SoTired for adding the LDFLAGS support_________________EASY TO INSTALL = Difficult to install, but instruction manual has pictures.
Join the adopt an unanswered post initiative today

Last edited by infirit on Fri May 14, 2010 11:40 am; edited 72 times in total

Well, actually KDE already uses --as-needed by default, if not disabled (for example on *BSD where it doesn't work too well).

This approach generally works well.. but still there ae cases in which packages got broken by --as-needed.. see for example x11-libs/gtk+-2.6.4-r1 which fails under amd64 for some conflicts between pic, amd64 relocations and --as-needed.

is this supposed to be a speed improvment in the time to compile or will it result in a faster binary when done?_________________Abit KD7-S
Athlon XP2500+
166mHz FSB
512 Meg PC3200 Ram running at 166mHz
LiteOn DVD dual Layer burner(hdc)

so if I wanted to recompile just one app with this option does it get added to the cflags variable or the ldflags variable? I have never changed the ldflags in make.conf_________________Abit KD7-S
Athlon XP2500+
166mHz FSB
512 Meg PC3200 Ram running at 166mHz
LiteOn DVD dual Layer burner(hdc)

if this is to speed up the launch of a program, how does this differ from pre-linking?_________________Abit KD7-S
Athlon XP2500+
166mHz FSB
512 Meg PC3200 Ram running at 166mHz
LiteOn DVD dual Layer burner(hdc)

Maybe the title of this thread could have been "compiling '--as-needed' / speeding up Gnome". such a title would almost immediately tell me we're doing specific thing X in hopes of specific result Y

"story on OSnews.com" tells me absolutely nothing worthwhile. sorry to be so anal...but I feel it's the duty of a poster to make a concise, useful thread title to aid in the de-obfuscation of these forums, allowing people to search them more quickly--there is a TON of information to go through.
How many times have you seen threads with completely vague or ambiguous titles like "problem with X.org" or "stupid question..." or "help me please" ? those do a less than ideal job of directing people to a.) problems they could provide answers for, or b.) problems they need help with.
</soapbox> (infirit, no offense to you)

if this is to speed up the launch of a program, how does this differ from pre-linking?

With prelinking the program still loads the same amount of libraries when you run it, but it loads them faster. This eliminates loading of unnecessary libraries.
But it can cause problems for some programs apparently. I used it on programs that I find realy annoyingly slow to load: gedit, gnome-console, gaim, totem, rhythmbox. None of them have any problems.
So I guess we should not put it into make.conf. Can someone tell me how to set it, so that portage will only use it on specific programs?

But it can cause problems for some programs apparently. I used it on programs that I find realy annoyingly slow to load: gedit, gnome-console, gaim, totem, rhythmbox. None of them have any problems.
So I guess we should not put it into make.conf. Can someone tell me how to set it, so that portage will only use it on specific programs?

I got into problems with quite a few programs: Totem, Sound-Juicer, basically all the GStreamer stuff, also gnome-system-tools and evince. On the other hand it seems to really speed up startup of some other stuff...

Maybe the title of this thread could have been "compiling '--as-needed' / speeding up Gnome". such a title would almost immediately tell me we're doing specific thing X in hopes of specific result Y
</soapbox> (infirit, no offense to you)

None taken

i have change the topic title to LDFLAGS --as-needed / speeding up Gnome"

I am going to take some measurements this wekeend for a couple of large program (large = bigger than gnome-terminal). I'll report back with some results. Any suggestions?_________________EASY TO INSTALL = Difficult to install, but instruction manual has pictures.
Join the adopt an unanswered post initiative today

I would like to point out that --as-needed is not only good for GNOME. Although it was said that KDE already uses --as-needed, I saw a difference in the number of libs linked against Konsole as shown below.

I have also built gimp with --as-needed and it seems to be working fine. I rebuilt KDE's konsole (I am using the splitted ebuilds). Using -Wl,--as-needed as a compiler flag did NOT work for Konsole, I had to use the following as was suggested above:

Code:

LDFLAGS="$LDFLAGS -Wl,--as-needed"

Using the below code to compare the number of libs linked, I got the following results for Konsole:
Konsole WITHOUT --as-needed links to 63 libs.
Konsole WITH --as-needed links to 26 libs.

This just uses a flat file "/etc/portage/package_as_needed" to check for packages that work when compiled with "-Wl,--as-needed" and chooses the appropriate method.

From what I read, those applications that have a plugin architecture are doomed to fail with this optimisation (i.e. apache) whilst many desktop apps that are fairly self contained can be optimised quite heavily.