如何更新到 C++11?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9271637/
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 12:34:28  来源:igfitidea点击:

How to update to C++11?

c++macosunixgccc++11

提问by fdh

I am new to programming, so have never experienced a language update. With the release of C++11, I want to make use of all the new features such as lambda expressions and threads. I know I can do this with external libraries but using native language features would be more convenient.

我是编程新手,所以从未经历过语言更新。随着 C++11 的发布,我想利用所有新功能,例如 lambda 表达式和线程。我知道我可以使用外部库来做到这一点,但使用本地语言功能会更方便。

I am using gcc 4.2.1 on Mac OS X Snowleopard in Xcode 3.2.6

我在 Xcode 3.2.6 的 Mac OS X Snowleopard 上使用 gcc 4.2.1

What all do I need to do and update to start using C++11 features?

我需要做什么和更新才能开始使用 C++11 功能?

回答by bames53

You can update to Xcode 4.1 (or whatever the most recent version you can get for Snow Leopard is) and get a new compiler with a few more C++11 features. There are some posts here on Stack Overflow about getting better support for C++11 in Xcode 4.1 on Snow Leopard.

您可以更新到 Xcode 4.1(或您可以为 Snow Leopard 获得的任何最新版本)并获得具有更多 C++11 功能的新编译器。Stack Overflow 上有一些关于在雪豹上的 Xcode 4.1 中更好地支持 C++11 的帖子。

But even the latest compiler available through Xcode does not support some C++11 features like lambdas. To get the best C++11 support you'll want to install a newer compiler, gcc 4.6 or 4.7, or Clang.

但即使是通过 Xcode 提供的最新编译器也不支持某些 C++11 特性,如 lambdas。要获得最佳的 C++11 支持,您需要安装更新的编译器、gcc 4.6 或 4.7 或 Clang。

I frequently build the latest version of clang from source. It's not difficult to do if you're familiar with building other open source software. I use the git repos for clang and llvm, http://llvm.org/git/llvm.gitand http://llvm.org/git/clang.git. You can also find instructions on their website for getting started: http://clang.llvm.org/get_started.html. Once you have the source for clang and llvm it's just ./configure && make && sudo make install. (you might want to run the tests before installing, since this is directly out of the repository. After makedo make checkin the llvm directory, and once that passes cd down to tools/clang and run make test. If everything is okay then sudo make install)

我经常从源代码构建最新版本的 clang。如果您熟悉构建其他开源软件,这并不难。我将 git repos 用于 clang 和 llvm,http://llvm.org/git/llvm.git以及http://llvm.org/git/clang.git. 您还可以在他们的网站上找到入门说明:http: //clang.llvm.org/get_started.html。一旦你拥有了 clang 和 llvm 的源代码,它就是./configure && make && sudo make install. (您可能需要运行安装前测试,因为这是直接从仓库中。后makemake check的LLVM目录中,一旦通过CD到工具/铛和运行make test。如果一切正常,然后sudo make install

I don't remember if Snow Leopard included libc++ or not, so you may need to get that as well. http://libcxx.llvm.org/

我不记得 Snow Leopard 是否包含 libc++,因此您可能也需要获取它。http://libcxx.llvm.org/

Once everything is built and installed you can do:

构建并安装完所有内容后,您可以执行以下操作:

clang++ -std=c++11 -stdlib=libc++ main.cpp && ./a.out

and you should have just about the best C++11 support around.

你应该有最好的 C++11 支持。

Recent patches in clang have really improved support for the last features you're likely to notice as a new C++ programmer. There are still a few bits and pieces left, but as of 3.1, and as far as I'm aware, clang has every C++11 feature that either gcc 4.7 or VC++11 has and more besides. libc++ also has the fewest gaps in terms of C++11 standard library features IME (though I think VC++'s standard library will also be pretty complete once they catch up on language features, e.g. char32_t and char16_t as native types so that the standard's mandated specializations for those types can be used).

clang 的最新补丁确实改进了对您作为新 C++ 程序员可能会注意到的最后一个功能的支持。还有一些零碎的东西,但从 3.1 开始,据我所知,clang 具有 gcc 4.7 或 VC++11 所具有的所有 C++11 特性,以及更多。libc++ 在 C++11 标准库特性 IME 方面也有最少的差距(尽管我认为 VC++ 的标准库一旦赶上语言特性也会非常完整,例如 char32_t 和 char16_t 作为本机类型,以便标准的强制专业化可以使用这些类型)。

回答by David Rodríguez - dribeas

Basically you only need to get a toolchain that has support for the new features. In macosx that would be either GCC or clang++. You might need to download/compile a particular version if needed (i.e. if the feature that you want to try is not in a prepacked compiler bundle but is available in the repository).

基本上,您只需要获得一个支持新功能的工具链。在 macosx 中,这将是 GCC 或 clang++。如果需要,您可能需要下载/编译特定版本(即,如果您想尝试的功能不在预打包的编译器包中,但在存储库中可用)。

I downloaded and compiled the latest trunk of clang++ from subversion to do some testing, and installed g++ 4.6 with macports in Snow Leopard. I remember that I had some issues with the setup of the environment, but don't quite remember which of the compilers gave me problems or how I solved them. Google is your friend there :)

我从subversion下载并编译了最新的clang++主干做一些测试,并在Snow Leopard中安装了带有macports的g++ 4.6。我记得我在设置环境时遇到了一些问题,但不太记得是哪个编译器给了我问题,或者我是如何解决这些问题的。谷歌是你的朋友 :)

回答by Spectral

If you have homebrew installed, from this article, just two steps:

如果您安装了自制软件,从本文开始,只需两个步骤:

brew tap homebrew/dupes
brew install gcc --enable-cxx --enable-fortran --use-llvm