C++ 什么时候需要使用标志 -stdlib=libstdc++?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19774778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:08:21  来源:igfitidea点击:

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

c++gccc++11stdlibstdc++

提问by Raymond Valdes

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

-stdlib=libstdc++用gcc编译时什么时候需要使用编译器和链接器的标志?

Does the compiler automatically use libstdc++?

编译器会自动使用 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++11to the compiler.

我在 Ubuntu 13.10 上使用 gcc4.8.2,我想使用 c++11 标准。我已经传递-std=c++11给编译器了。

回答by Bill Lynch

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:

在 Linux 上:一般来说,所有常用的 linux 发行版都会默认使用 libstdc++,并且所有现代版本的 GCC 都带有支持 C++11 的 libstdc++。如果要在此处编译 c++11 代码,请使用以下方法之一:

  • g++ -std=c++11 input.cxx -o a.out(usually GNU compiler)
  • g++ -std=gnu++11 input.cxx -o a.out
  • g++ -std=c++11 input.cxx -o a.out(通常是 GNU 编译器)
  • 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:

在 Mavericks 之前的 OS X 上:g++实际上是别名,clang++而 Apple 的旧版本 libstdc++ 是默认值。您可以通过传递-stdlib=libc++. 如果要在此处编译 c++11 代码,请使用以下方法之一:

  • 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
  • g++ -std=c++11 -stdlib=libc++ input.cxx -o a.out(clang,不是 GNU 编译器!)
  • g++ -std=gnu++11 -stdlib=libc++ input.cxx -o a.out(clang,不是 GNU 编译器!)
  • 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++

自小牛队以来在 OS X 上:libc++ 是默认设置。您可以通过传递使用 Apple 的旧版本 libstdc++(不包括 c++11 库支持)-stdlib=libstdc++

  • clang++ -std=c++11 input.cxx -o a.out
  • clang++ -std=gnu++11 input.cxx -o a.out
  • clang++ -std=c++11 input.cxx -o a.out
  • clang++ -std=gnu++11 input.cxx -o a.out

回答by Jonathan Wakely

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

-stdlib=libstdc++用gcc编译时什么时候需要使用编译器和链接器的标志?

Short answer: never

简短回答:从不

Longer answer: -stdlibis a Clang flag and will not work with any version of GCC ever released. On Mac OS X sometimes the gccand g++commands are actually aliases for Clang notGCC, 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 gccand g++really are GCC not Clang, and so the -stdliboption won't work at all.

更长的答案:-stdlib是一个 Clang 标志,不适用于曾经发布的任何版本的 GCC。在 Mac OS X 上,有时gccg++命令实际上是 Clang而非GCC 的别名,而且 Apple 发布的 libstdc++ 版本很古老(大约 2008 年),因此它当然不支持 C++11。这意味着在 OS X 上使用 Clang-pretending-to-be-GCC 时,可以使用-stdlib=libc++选择 Clang 的新的 C++11 兼容库,也可以使用-stdlib=libstdc++选择 libstdc++ 的 pre-C++11 古董版本那属于博物馆。但是在 GNU/Linux 上gccg++确实是 GCC 而不是 Clang,因此该-stdlib选项根本不起作用。

Does the compiler automatically use libstdc++?

编译器会自动使用 libstdc++ 吗?

Yes, GCC always uses libstdc++ unless you tell it to use nostandard library at all with the -nostdliboption (in which case you either need to avoid using any standard library features, or use -Iand -Land -lflags to point it to an alternative set of header and library files).

是的,GCC 总是使用 libstdc++,除非您通过选项告诉它根本不使用标准库-nostdlib(在这种情况下,您要么需要避免使用任何标准库功能,要么使用-Iand-L-l标志将其指向另一组标头和库文件)。

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++11to the compiler.

我在 Ubuntu 13.10 上使用 gcc4.8.2,我想使用 c++11 标准。我已经传递-std=c++11给编译器了。

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++11then that enables the C++11 features in g++compiler and also the C++11 features in the libstdc++ headers.

你不需要做任何其他事情。GCC 带有自己的 C++ 标准库 (libstdc++) 实现,它是与 GCC 本身一起开发和测试的,因此 GCC 的版本和 libstdc++ 的版本是 100% 兼容的。如果您使用编译器进行编译,-std=c++11则会启用g++编译器中的 C++11 功能以及 libstdc++ 头文件中的 C++11 功能。

回答by Torsten Robitzki

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

如果您使用 g++ 前端,而不是 gcc 前端,编译器会自动使用 libstdc++。