This is an archive of the discontinued LLVM Phabricator instance.

Introduce -nostdlib++ flag to disable linking the C++ standard library.
ClosedPublic

Authored by thakis on Jul 23 2017, 11:55 AM.

Details

Reviewers
hans
Summary

Projects that want to statically link their own C++ standard library currently need to pass -nostdlib or -nodefaultlibs, which also disables linking of the builtins library, -lm, and so on. Alternatively, they could use clang instead of clang++, but that already disables implicit addition of -lm on some toolchains.

Add a dedicated flag -nostdlib++ that disables just linking of libc++ / libstdc++. This is analogous to -nostdinc++.

Diff Detail

Event Timeline

thakis created this revision.Jul 23 2017, 11:55 AM

Most of the patch is unifying all the toolchains to call the newly-introduced ToolChain::ShouldLinkCXXStdlib() instead of all manually checking for D.CCIsCXX() && !getFlag(nostdlib, nodefaultlibs). The actual behavior change is to make that function check the new nostdlib++ flag too.

One use-case for this new flag is https://cs.chromium.org/chromium/src/build/config/c%2B%2B/BUILD.gn?type=cs&q=nodefaultlibs&sq=package:chromium&l=32

joerg added a subscriber: joerg.Jul 23 2017, 12:07 PM

I don't really like this. The reason why -lm is added explicitly on many targets is because the C++ STL typically depends on it and that means for static linking and broken ELF linkers, it will be necessary to link against it explicitly.
There is also the question on whether any platform we have currently uses separate STL and ABI libraries and it is not clear whether the flag should handle both.

hans accepted this revision.Jul 25 2017, 10:23 AM

This seems reasonable to me.

lib/Driver/ToolChain.cpp
660

The message here doesn't really add any info. I think asserting without a message in this case is fine.

This revision is now accepted and ready to land.Jul 25 2017, 10:23 AM
thakis closed this revision.Jul 25 2017, 11:03 AM

r308997, thanks!

Herald added a project: Restricted Project. · View Herald TranscriptSep 22 2023, 5:00 AM