如何在Mac Terminal中使用C ++ 11支持编译C ++ [英] How to compile C++ with C++11 support in Mac Terminal

查看:540
本文介绍了如何在Mac Terminal中使用C ++ 11支持编译C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Mac Terminal中编译C ++ 11源代码但失败。我试过 g ++ -std = c ++ 11 g ++ -std = c ++ 0x g ++ -std = gnu ++ 11 g ++ -std = gnu ++ 0x 但是没有任何工作。终端始终读取无法识别的命令行选项。然而, g ++ -std = gnu 之类的东西可以正常工作(当然C ++ 11源代码无法通过)。



我应该使用哪个选项来打开C ++ 11支持?



顺便提一句,我使用的命令行工具安装在Xcode ,我很确定它们是最新的。

code> clang ++ 而不是 g ++ 。另外,您应该使用libc ++库而不是默认的libstdc ++;包含的libstdc ++版本很旧,因此不包括C ++ 11库的功能。
$ b

  clang ++ -std = c + +11 -stdlib = libc ++ -Weverything main.cpp 

如果您尚未安装命令行工具对于Xcode,您可以通过使用 xcrun 工具来运行编译器和其他工具。

  xcrun clang ++ -std = c ++ 11 -stdlib = libc ++ -Weverything main.cpp 

另外,如果您想禁用特定的警告,则可以将其他标志传递给编译器来执行此操作。在警告消息结束时,它向您显示将启用警告的最具体的标志。要禁用该警告,请在警告名称前添加 no -

例如,您可能不希望c ++ 98兼容性警告。在这些警告结束时,它显示标志 -Wc ++ 98-compat 并禁用它们,您将 -Wno-c ++ 98- compat


I wanted to compile C++11 source code within Mac Terminal but failed. I tried g++ -std=c++11, g++ -std=c++0x, g++ -std=gnu++11 and g++ -std=gnu++0x but nothing worked. Terminal always read unrecognized command line option. However, g++ -std=gnu and things like that worked fine (of course C++11 source code could not pass).

Which option should I use to turn on C++11 support?

By the way, the command line tool I'm using is installed within Xcode, and I'm pretty sure that they are up-to-date.

解决方案

As others have pointed out you should use clang++ rather than g++. Also, you should use the libc++ library instead of the default libstdc++; The included version of libstdc++ is quite old and therefore does not include C++11 library features.

clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp

If you haven't installed the command line tools for Xcode you can run the compiler and other tools without doing that by using the xcrun tool.

xcrun clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp

Also if there's a particular warning you want to disable you can pass additional flags to the compiler to do so. At the end of the warning messages it shows you the most specific flag that would enable the warning. To disable that warning you prepend no- to the warning name.

For example you probably don't want the c++98 compatibility warnings. At the end of those warnings it shows the flag -Wc++98-compat and to disable them you pass -Wno-c++98-compat.

这篇关于如何在Mac Terminal中使用C ++ 11支持编译C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆