我可以在 Xcode 中使用 C++11 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4574246/
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
Can I use C++11 with Xcode?
提问by villintehaspam
I am considering the use of some C++11 features (like auto
for instance) in some cross-platform projects (Windows+Mac). On Windows, Visual Studio supports parts of the upcoming C++11 standard that would allow me to simplify parts of the code base so naturally I would be interested in starting to use these features.
我正在考虑auto
在一些跨平台项目(Windows+Mac)中使用一些 C++11 特性(例如)。在 Windows 上,Visual Studio 支持即将推出的 C++11 标准的部分内容,这将允许我简化代码库的部分内容,因此我自然会对开始使用这些功能感兴趣。
But as far as I am aware, the current XCode version (3.2.4 + GCC 4.2) does not support any C++11 features at all. Can I upgrade the GCC version or the CLang version somehow? Or should I just bite my tongue and wait for Apple to package a new version sometime in the future?
但据我所知,当前的 XCode 版本(3.2.4 + GCC 4.2)根本不支持任何 C++11 功能。我可以以某种方式升级 GCC 版本或 CLang 版本吗?或者我应该咬紧牙关,等待苹果在未来某个时候打包一个新版本?
采纳答案by RichardLiu
Xcode 4.2 had finally added support for C++0X:
Xcode 4.2 终于添加了对 C++0X 的支持:
In the project build settings screen, switch on "All" options.
In the "Build Options" section, set compiler to "Apple LLVM compiler 3.0".
Scroll down to "Apple LLVM Compiler 3.0 - Language" section and set "C++ Language Dialect" to "C++0X" and "C++ Standard Library" to "libc++".
在项目构建设置屏幕中,打开“所有”选项。
在“构建选项”部分,将编译器设置为“Apple LLVM 编译器 3.0”。
向下滚动到“Apple LLVM Compiler 3.0 - Language”部分,将“C++ Language Dialect”设置为“C++0X”,将“C++ Standard Library”设置为“libc++”。
The std::move(), move constructor and R-Value reference are known to work as expected, and I'm testing on the std::thread and std::atomic.
已知 std::move()、移动构造函数和 R-Value 引用按预期工作,我正在测试 std::thread 和 std::atomic。
回答by justin
======= Update 2012: =======
======= 2012 年更新:=======
Start with Clang - Many C++11 features are now available in Clang. It's included with Xcode.
从 Clang 开始 - Clang 现在提供了许多 C++11 功能。它包含在 Xcode 中。
======= Original answer from Jan 2011: =======
======== 2011 年 1 月的原始答案:=======
intel's compiler may be the cleanest way to go at this time. http://software.intel.com/en-us/articles/intel-composer-xe/
英特尔的编译器可能是目前最干净的方法。 http://software.intel.com/en-us/articles/intel-composer-xe/
clang's promising, but not particularly stable or featured wrt c++0x features. c++ is still very new for clang.
clang 很有前途,但不是特别稳定或特色 wrt c++0x 功能。c++ 对 clang 来说还是很新的。
gcc: relatively mature, but you'll have to write and maintain your compiler plugins for xcode.
gcc:相对成熟,但你必须为 xcode 编写和维护你的编译器插件。
you can also specify custom scripts, but that is a pain to maintain... unless you go all out and create an adaptor tool.
您还可以指定自定义脚本,但维护起来很痛苦……除非您全力以赴创建一个适配器工具。
回答by Barry Wark
回答by centaurian_slug
I've found auto, decltype(), range based "for (:)" work in a cross platform project (LLVM for MacOSX,iOS, gcc/linux, MSVC 10/windows).
我在跨平台项目(适用于 MacOSX、iOS、gcc/linux、MSVC 10/windows 的 LLVM)中发现了 auto、decltype()、基于范围的“for (:)”。
lambdas & variadic macros don't appear to work under LLVM yet sadly.
遗憾的是,lambdas 和可变参数宏似乎无法在 LLVM 下工作。