When is it necessary to use the flag -stdlib=libstdc++?
Asked Answered
D

3

92

When is it necessary to use use the flag -stdlib=libstdc++ for the compiler and linker when compiling with gcc?

Does the compiler automatically use libstdc++?

I am using gcc4.8.2 on Ubuntu 13.10 and I would like to use the c++11 standard. I already pass -std=c++11 to the compiler.

Dejection answered 4/11, 2013 at 18:43 Comment(1)
Err, old question I know, but -stdlib=libstdc++ is not a valid gcc flag. It is usable on MacOS only because g++ on MacOS is actually clang++.Retharethink
B
114

On Linux: In general, all commonly available linux distributions will use libstdc++ by default, and all modern versions of GCC come with a libstdc++ that supports C++11. If you want to compile c++11 code here, use one of:

  • g++ -std=c++11 input.cxx -o a.out (usually GNU compiler)
  • g++ -std=gnu++11 input.cxx -o a.out

On OS X before Mavericks: g++ was actually an alias for clang++ and Apple's old version of libstdc++ was the default. You could use libc++ (which included c++11 library support) by passing -stdlib=libc++. If you want to compile c++11 code here, use one of:

  • g++ -std=c++11 -stdlib=libc++ input.cxx -o a.out (clang, not GNU compiler!)
  • g++ -std=gnu++11 -stdlib=libc++ input.cxx -o a.out (clang, not GNU compiler!)
  • clang++ -std=c++11 -stdlib=libc++ input.cxx -o a.out
  • clang++ -std=gnu++11 -stdlib=libc++ input.cxx -o a.out

On OS X since Mavericks: libc++ is the default and you should not pass any -stdlib=<...> flag. Since Xcode 10, building against libstdc++ is not supported at all anymore. Existing code built against libstdc++ will keep working because libstdc++.6.dylib is still provided, but compiling new code against libstdc++ is not supported.

  • clang++ -std=c++11 input.cxx -o a.out
  • clang++ -std=gnu++11 input.cxx -o a.out
Bedazzle answered 4/11, 2013 at 18:52 Comment(12)
If I understand this correctly, then libstdc++ supports c++11 for linux but not OSX10.9. -> c++11 code on osx10.9 must be compiled/linked with -stdlib=libc++.Dejection
@raymondvaldes: That's correct. Apple refuses to distribute newer versions of libstdc++ that would contain C++11 support.Bedazzle
Can I use libstdc++ with clang (not clang++)?Anguilla
@hithwen: libstdc++ is the C++ standard library. It doesn't make much sense to use it with clang instead of clang++.Bedazzle
@hithwen: I don't think your problem is related to this answer. I'd recommend creating a new question to ask this.Bedazzle
What's the difference between libc++ and libstdc++? Are they both c++ standard runtime library?Steelman
@Steelman yes. They are two different implementations of the c++ standard library. One by the gcc folks, one by the llvm folks.Bedazzle
To avoid the silly "g++" and "gcc" nonsense on OS X, it's probably time-saving to explicitly use g++-mp-9 (or whatever flavor of GCC install has been performed) during compilation, if that's the goal - in which case the real GNU libstdc++ library will be used by the real GNU G++ compiler, not the old OS X "libstdc++" (or newer libc++) library on faked compatibility-shim "g++".Danika
This answer is good, but it should be updated to say that as of recently, -stdlib=libstdc++ should simply not be used on macOS, since libstdc++ is not shipped in the Apple SDKs anymore.Arrest
@LouisDionne: I wrote the original answer a decade ago. Feel free to edit it as appropriate.Bedazzle
@LouisDionne What about static linkage?Herman
But for building boost on macOS ARM I apparently need to pass for -std=c++11 also -stdlib=libc++ or it segfaults although otool -L shows libc++ either way. See also here: https://mcmap.net/q/183325/-segmentation-fault-with-boost-filesystem any idea why?Hom
H
38

When is it necessary to use use the flag -stdlib=libstdc++ for the compiler and linker when compiling with gcc?

Short answer: never

Longer answer: -stdlib is a Clang flag and will not work with any version of GCC ever released. On macOS sometimes the gcc and g++ commands are actually aliases for Clang not GCC, and the version of libstdc++ that Apple ships is ancient (circa 2008) so of course it doesn't support C++11. This means that on macOS when using Clang-pretending-to-be-GCC, you can use -stdlib=libc++ to select Clang's new C++11-compatible library, or you can use -stdlib=libstdc++ to select the pre-C++11 antique version of libstdc++ that belongs in a museum. But on GNU/Linux gcc and g++ really are GCC not Clang, and so the -stdlib option won't work at all.

Edit: Since I wrote this answer, GCC was changed to conditionally support the -stdlib flag, but for most platforms that support is disabled by default. Even when it's enabled, the default is -stdlib=libstdc++ so you still never need to say that explicitly. GCC will still automatically use libstdc++.

Does the compiler automatically use libstdc++?

Yes, GCC always uses libstdc++ unless you tell it to use no standard library at all with the -nostdlib or -nostdlib++ option (in which case you either need to avoid using any standard library features, or use -I and -L and -l flags to point it to an alternative set of header and library files).

I am using gcc4.8.2 on Ubuntu 13.10 and I would like to use the c++11 standard. I already pass -std=c++11 to the compiler.

You don't need to do anything else. GCC comes with its own implementation of the C++ standard library (libstdc++) which is developed and tested alongside GCC itself so the version of GCC and the version of libstdc++ are 100% compatible. If you compile with -std=c++11 then that enables the C++11 features in g++ compiler and also the C++11 features in the libstdc++ headers.

Hitlerism answered 18/5, 2018 at 9:3 Comment(7)
I used clang in my project, but for CI the compiler is set to gcc by default. So this is exactly the problem that occurred. gcc doesn't support -stdlib so the CI always failedCaretaker
@JonathanWakely "or use -I and -L and -l flags to point it to an alternative set of header and library files" -- per your suggestion I'm using -l:libstdc++.so.6.0.21 to override a .19 version, ldd finds that, but unless the /usr/lib64/libstdc++.so.6 symlink points to the .21 version, the .21 symbols are ignored. How can to get around the symlink ?Pteranodon
@JeffBrower that's a completely different question and doesn't belong in a comment here. Short answer: why are you doing that? Don't do that, it's not a valid way to use GCC or libstdc++. Longer answer: You can't, you need to make the dynamic loader (ld.so) find a different libstdc++.so.6 in a different directory, where it's a symlink to the other version, as per gcc.gnu.org/onlinedocs/libstdc++/manual/… -- but why are you doing that? Don't do that, that's not a valid way to use GCC or libstdc++.Hitlerism
@JonathanWakely then I misunderstood your advice, my apologiesPteranodon
@JeffBower if you provide alternative headers and libraries they still need to match the version of gcc. Mixing the compiler of GCC 11 with headers or libs from GCC 12 (or vice versa) isn't supported.Hitlerism
It's not true that it's never needed. For some reason, when building boost on Apple ARM it is needed or I will get segfault although otool -L shows usage of libc++ either way, see also: https://mcmap.net/q/183325/-segmentation-fault-with-boost-filesystemHom
@Hom that answer says you need to use -stdlib=libc++ with clang, this question is asking when you need to use -stdlib=libstdc++ with gcc. Not the same thing at all.Hitlerism
S
7

The compiler uses the libstdc++ automatically, if you use the g++ frontend, not the gcc frontend.

Saleme answered 4/11, 2013 at 18:46 Comment(5)
Indeed. Just to clarify: The OP asked "when compiling with gcc?". If one runs gcc on a bunch of .o files, then I think it assume they're just C programs and it doesn't link in any C++ stuff. But if you use g++ (or if you have any cpp files on the gcc command line), then I think it'll realise that stdc++ should be included. But I'm not too certain about this. Is this what you're saying?Methionine
To clarify my question, when I said gcc I meant the gnu compiler collection (as a whole). Since I'm talking about c++ code, then I would be using the g++ frontend.Dejection
Wow. This one was not obvious.Underfeed
@RaymondValdes when referring to the whole it's conventional to say GCC, to distinguish it from the gcc driver program.Hitlerism
@Torsten, what you're referring to is automatically linking to libstdc++ if you use g++ rather than gcc. If you compile a C++ file (one with an extension like .cc or .C or .cpp) with gcc then it compiles the code using the C++ front-end and automatically makes the libstdc++ headers available via #include, exactly the same as when you compile with g++. Only the linking step handles libstdc++ differently depending whether you use gcc or g++.Hitlerism

© 2022 - 2024 — McMap. All rights reserved.