[英]When is it necessary to use the flag -stdlib=libstdc++?
When is it necessary to use use the flag -stdlib=libstdc++
for the compiler and linker when compiling with gcc?使用 gcc 进行编译时,何时需要为编译器和链接器使用标志
-stdlib=libstdc++
?
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.我在 Ubuntu 13.10 上使用 gcc4.8.2,我想使用 c++11 标准。 I already pass
-std=c++11
to the compiler.我已经将
-std=c++11
传递给编译器。
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.在 Linux 上:一般来说,所有常用的 linux 发行版都会默认使用 libstdc++,并且所有现代版本的 GCC 都带有支持 C++11 的 libstdc++。 If you want to compile c++11 code here, use one of:
如果要在此处编译 c++11 代码,请使用以下方法之一:
g++ -std=c++11 input.cxx -o a.out
(usually GNU compiler) 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.在 Mavericks 之前的 OS X 上:
g++
实际上是clang++
的别名,而 Apple 的旧版本 libstdc++ 是默认值。 You could use libc++ (which included c++11 library support) by passing -stdlib=libc++
.您可以通过传递
-stdlib=libc++
来使用 libc++(包括 c++11 库支持)。 If you want to compile c++11 code here, use one of:如果要在此处编译 c++11 代码,请使用以下方法之一:
g++ -std=c++11 -stdlib=libc++ input.cxx -o a.out
(clang, not GNU compiler!) 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, not GNU compiler!) 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.自小牛队以来在 OS X 上:libc++ 是默认设置。 You can use Apple's old version of libstdc++ (which does not include c++11 library support) by passing
-stdlib=libstdc++
您可以通过传递
-stdlib=libstdc++
来使用 Apple 的旧版本 libstdc++(不包括 c++11 库支持)
clang++ -std=c++11 input.cxx -o a.out
clang++ -std=gnu++11 input.cxx -o a.out
When is it necessary to use use the flag
-stdlib=libstdc++
for the compiler and linker when compiling with gcc?使用 gcc 进行编译时,何时需要为编译器和链接器使用标志
-stdlib=libstdc++
?
Short answer: never简短回答:从不
Longer answer: -stdlib
is a Clang flag and will not work with any version of GCC ever released.更长的答案:
-stdlib
是一个 Clang 标志,不适用于任何已发布的 GCC 版本。 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.在 Mac OS X 上,有时
gcc
和g++
命令实际上是 Clang而非GCC 的别名,而且 Apple 发布的 libstdc++ 版本很古老(大约 2008 年),因此它当然不支持 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.这意味着在 OS X 上使用 Clang-pretending-to-be-GCC 时,可以使用
-stdlib=libc++
来选择 Clang 新的 C++11 兼容库,或者可以使用-stdlib=libstdc++
来选择预属于博物馆的 C++11 古董版 libstdc++。 But on GNU/Linux gcc
and g++
really are GCC not Clang, and so the -stdlib
option won't work at all.但是在 GNU/Linux 上,
gcc
和g++
确实是 GCC 而不是 Clang,因此-stdlib
选项根本不起作用。
Does the compiler automatically use libstdc++?
编译器会自动使用 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).是的,GCC 总是使用 libstdc++,除非您通过
-nostdlib
选项告诉它根本不使用标准库(在这种情况下,您需要避免使用任何标准库功能,或者使用-I
和-L
和-l
标志来指向到另一组头文件和库文件)。
I am using gcc4.8.2 on Ubuntu 13.10 and I would like to use the c++11 standard.
我在 Ubuntu 13.10 上使用 gcc4.8.2,我想使用 c++11 标准。 I already pass
-std=c++11
to the compiler.我已经将
-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.
GCC 带有自己的 C++ 标准库 (libstdc++) 实现,它是与 GCC 本身一起开发和测试的,因此 GCC 的版本和 libstdc++ 的版本是 100% 兼容的。 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.如果您使用
-std=c++11
进行编译,那么这将启用g++
编译器中的 C++11 功能以及 libstdc++ 头文件中的 C++11 功能。
如果您使用 g++ 前端,而不是 gcc 前端,编译器会自动使用 libstdc++。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.