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
The story
How to filter the flags in ebuildsToday 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.
There is a way to have portage filter this flag at compile time:
Add the following to the top of the ebuild below IUSE
Code: Select all
inherit flag-o-maticCode: Select all
# This LDFLAG breaks "package name"
filter-ldflags "-Wl,--as-needed"
filter-cflags "-Wl,--as-needed"There is another LDFLAG which is more safe then plain --as-needed. The flag is --enable-new_ldflags and devsk eplains it best
I could not work out if this works or not, tested on konsoledefsk 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.
LDFLAG Central
Some interesting ldflags in this post
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









