On Aug 2, 2018, at 2:57 AM, Jakub Jelinek jakub@redhat.com wrote:
On Thu, Aug 02, 2018 at 11:45:00AM +0200, Dominique Martinet wrote: Wearing a lib developer hat, I don't see how you can make a .pc that doesn't overlink if you provide something a bit entangled with other libs. The problem is that if your headers use any sub-lib type you need to add that lib in Requires: so that --cflags will pull that lib's include path; and that will in turn add that sub-lib to the linked libs even if the program likely doesn't care about it (either because they didn't even use that part of your lib, or because all uses of that lib will be done through your own anyway so it wasn't required in the first place)
Then it is clearly a task for pkg-config (and libtool) to handle it better. If the --libs it provides contains some mandatory and some optionally needed libraries, then it should differentiate between them and use -Wl,--push-state,--as-needed ... -Wl,--pop-state around those that are optionally needed. If all libraries from those tools are optional, perhaps it should use it always.
Changing the behavior of say -lpthread on the command line is a bad idea, many projects really expect it to mean that the mentioned library is linked in and if it no longer does, it causes silent breakage. Forcing users to do -Wl,--push-state,--no-as-needed ... -Wl,--pop-state whenever they really mean to link some library is too hostile.
Maybe pkg-config could learn to push state and use —as-needed? Some explicit options in gcc and ld like —link-as-needed=libname and —always-link=libname would be nice, too. It seems that the toolchain folks (like you) want -lpthread and the like to keep working, and the package people want a sane default where symbols are made available but the DT_NEEDED entry is only used if needed.
On the other hand, switching to as-needed linking seems like it's a step in the right direction in general, but ISTM it would be much nicer to switch to as-needed direct binding. Do glibc and binutils even support Solaris-style direct binding yet?