Game World!

Join A World Of Gamers

Enter your email address:

Delivered by FeedBurner

Followers

Popular Posts

Monday 28 June 2021

Does clang use Libstdc ++?

 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.

  • 6
    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++. – davmac Jan 8 '18 at 12:24
99

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 Mavericksg++ 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. You can use Apple's old version of libstdc++ (which does not include c++11 library support) by passing -stdlib=libstdc++

  • clang++ -std=c++11 input.cxx -o a.out
  • clang++ -std=gnu++11 input.cxx -o a.out
  • 1
    @raymondvaldes: That's correct. Apple refuses to distribute newer versions of libstdc++ that would contain C++11 support. – Bill Lynch Nov 4 '13 at 20:56
  • 1
    Can I use libstdc++ with clang (not clang++)? – hithwen Feb 8 '14 at 16:53
  • 6
    @hithwen: I don't think your problem is related to this answer. I'd recommend creating a new question to ask this. – Bill Lynch Feb 9 '14 at 14:40
  • 2
    What's the difference between libc++ and libstdc++? Are they both c++ standard runtime library? – nn0p Nov 27 '16 at 8:47 
  • 8
    @nn0p yes. They are two different implementations of the c++ standard library. One by the gcc folks, one by the llvm folks. – Bill Lynch Nov 27 '16 at 14:07
30

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 Mac OS X 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 OS X 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.

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 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.

  • 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 failed – tjysdsg May 12 '20 at 0:42 
7

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

  • 2
    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? – Aaron McDaid Nov 4 '13 at 18:57 
  • 1
    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. – Raymond Valdes Nov 4 '13 at 19:30
  • Wow. This one was not obvious. – garyF Nov 7 '17 at 15:42 
  • 1
    @RaymondValdes when referring to the whole it's conventional to say GCC, to distinguish it from the gcc driver program. – Jonathan Wakely May 18 '18 at 9:03 
  • 1
    @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++. – Jonathan Wakely May 18 '18 at 9:19 

Floating Button

Button