This is an archive of the discontinued LLVM Phabricator instance.

[Driver] Make AddCXXStdlibLibArgs responsible for handling -static-libstdc++.
Needs ReviewPublic

Authored by samsonov on Dec 16 2015, 5:34 PM.

Details

Summary

After this change, AddCXXStdlibLibArgs will check the presence of
-static-libstdc++ flag and, if necessary, wrap -lc++/-lstdc++ in
-Bstatic/-Bdynamic to force static linking of C++ standard library.

It's no longer necessary to copy this code around across toolchains
(unless toolchain is overriding AddCXXStdlibLibArgs).

This change has an important consequnce: if the user provides "-lstdc++"
manually (i.e. it's not added automatically when we're linking C++ binary),
then "-static-libstdc++" *will* work as expected.

Other than that, the change attempts to preserve the existing behavior.

Diff Detail

Event Timeline

samsonov updated this revision to Diff 43091.Dec 16 2015, 5:34 PM
samsonov retitled this revision from to [Driver] Make AddCXXStdlibLibArgs responsible for handling -static-libstdc++..
samsonov updated this object.
samsonov added reviewers: rafael, compnerd.
samsonov added a subscriber: cfe-commits.
rafael edited edge metadata.Dec 18 2015, 2:25 PM
rafael added a subscriber: rafael.

I am not sure what is "expected" is here:

$ gcc test.o -o test.so -lstdc++ -shared -static-libstdc++ && ldd
test.so | grep libst
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f848e57d000)

$ gcc test.o -o test.so -shared -static-libstdc++ -lstdc++ && ldd
test.so | grep libst
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fc25516d000)

echristo edited edge metadata.Dec 18 2015, 2:28 PM

What's "expected" here? Do we know which of the options are overriding? IIRC shared and the static options aren't necessarily last one wins with gcc.

-eric

I am not sure what is "expected" is here:

Interesting.
I was assuming that Clang tends to understand "-lstdc++" as a special argument that says "link against C++ standard library", not "link against libstdc++.{a,so}".
For instance,

clang a.cc -lstdc++ -stdlib=libc++

will effectively replace "-lstdc++" with "-lc++", and

clang++ a.cc -stdlib=libc++ -static-libstdc++

will link against libc++ statically. In that sense, it makes sense to assume that "-static-libstdc++" will bind to "-lstdc++" argument.

Apparently, it's not what GCC does :( Do you think we should keep being compatible here?

$ gcc test.o -o test.so -lstdc++ -shared -static-libstdc++ && ldd
test.so | grep libst
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f848e57d000)

$ gcc test.o -o test.so -shared -static-libstdc++ -lstdc++ && ldd
test.so | grep libst
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fc25516d000)

espindola edited reviewers, added: espindola; removed: rafael.Mar 15 2018, 10:56 AM