Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 35 |
Nodes: | 6 (1 / 5) |
Uptime: | 43:12:14 |
Calls: | 322 |
Calls today: | 1 |
Files: | 960 |
Messages: | 81,935 |
Posted today: | 3 |
A question about --as-needed as an upstream developer who wants to
care about more than just Debian or Linux. Am I correct in assuming
this will work on any system using GNU binutils? And it doesn't
matter whether you are using gcc, clang, etc. What about other OS's
such as *BSD, MacOS, etc.?
On Tue, Jan 21, 2025 at 10:31:29AM -0500, Theodore Ts'o wrote:
A question about --as-needed as an upstream developer who wants to
care about more than just Debian or Linux. Am I correct in assuming
this will work on any system using GNU binutils? And it doesn't
matter whether you are using gcc, clang, etc. What about other OS's
such as *BSD, MacOS, etc.?
I often consult Gnulib for lore on this kind of thing. https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=m4/lib-ignore.m4 seems to have some useful hints, with comments for those not fluent in
m4.
This also seems to be a standalone file, so if your project is using
Autoconf but not Gnulib, then you should be able to copy it into your project's local macro directory, add gl_IGNORE_UNUSED_LIBRARIES to configure.ac, and add $(IGNORE_UNUSED_LIBRARIES_CFLAGS) etc. to whatever *_LDFLAGS variable is appropriate.
I guess I could add an autoconf test to see if the linker barfs on --as-needed, but I'm curious how much this actually matters in
practice.
Some upstreams have gone through build system tests to assess whether
to build with libatomic or not. Adding -latomic is simpler. I wonder if
the build system configure stage test is overkill.
In my own packages, I check if libatomic exists, and if it does, I unconditionally link if I use any atomics. I also check if the linker
accepts --push-flags, if it does I generate a -Wl,--push-flags,--as-needed,-latomic,--pop-flags sequence, if not, the unconditional link will generate dpkg-shlibdeps warnings about unnecessary linking on amd64, but that's better than failing on other platforms.
Can you explain why this:
-Wl,--push-flags,--as-needed,-latomic,--pop-flags
is better than
-as-needed,-latomic
I thought --as-needed on its own is sufficicent to avoid dpkg-shlibdeps warnings
about unnecessary linking (because it only links the things that are needed), so
I don't understand the need for the psuh/pop sequence (I must admit that I never
knew that existed until your message recently).
In my own packages, I check if libatomic exists, and if it does, I unconditionally link if I use any atomics. I also check if the linker accepts --push-flags, if it does I generate a -Wl,--push-flags,--as-needed,-latomic,--pop-flags sequence, if not, the unconditional link will generate dpkg-shlibdeps warnings about unnecessary linking on amd64, but that's better than failing on other platforms.
I understand from the other mails that this architecture-check is not even needed and that --as-needed will let the linker do the right thing. I still like to special-case the only arch where this seems to be needed.
I gave up on
hoping that onetbb fixes it as neither upstream nor the Debian bug show any activity, so I'm patching my own package instead.
/usr/bin/ld: unrecognized option '--push-flags'
You probably meant --push-state and --pop-state instead?
Quoting Simon Richter (2025-01-17 11:43:39)
In my own packages, I check if libatomic exists, and if it does, I unconditionally link if I use any atomics. I also check if the linker accepts
--push-flags, if it does I generate a -Wl,--push-flags,--as-needed,-latomic,--pop-flags sequence, if not, the unconditional link will generate dpkg-shlibdeps warnings about unnecessary linking on amd64, but that's better than failing on other platforms.
thank you for your help! I now patched vcmi with snippets like this:
if (CMAKE_LIBRARY_ARCHITECTURE STREQUAL "arm-linux-gnueabi")
target_link_options(vcmi PRIVATE "-Wl,--push-flags,--as-needed,-latomic,--pop-flags")
endif()
I understand from the other mails that this architecture-check is not even needed and that --as-needed will let the linker do the right thing. I still like to special-case the only arch where this seems to be needed. I gave up on
hoping that onetbb fixes it as neither upstream nor the Debian bug show any activity, so I'm patching my own package instead.
But then I get the complaint:
/usr/bin/ld: unrecognized option '--push-flags'
You probably meant --push-state and --pop-state instead?
I suspect that the problem is the order in which the -latomic is added to the linker flags?
Because if instead of target_link_options() with push and
pop-state I just use
target_link_libraries(vcmiclient PRIVATE atomic)
Then that will add a -latomic right after ../bin/libvcmiclientcommon.a and then
the build succeeds.
So what is the canonical way to achieve the desired result with CMake? Will I instead have to globally set LDFLAGS="-Wl,--as-needed"?