如何在 Mac 终端中使用 C++11 支持编译 C++

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

How to compile C++ with C++11 support in Mac Terminal

c++macosc++11terminalg++

提问by 4ae1e1

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++11and g++ -std=gnu++0xbut nothing worked. Terminal always read unrecognized command line option. However, g++ -std=gnuand things like that worked fine (of course C++11 source code could not pass).

我想在 Mac 终端中编译 C++11 源代码但失败了。我试过g++ -std=c++11g++ -std=c++0xg++ -std=gnu++11g++ -std=gnu++0x,但毫无效果。终端总是读取unrecognized command line option. 然而,这样的g++ -std=gnu事情工作得很好(当然 C++11 源代码无法通过)。

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

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

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.

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

回答by bames53

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++而不是g++. 此外,您应该使用 libc++ 库而不是默认的 libstdc++;包含的 libstdc++ 版本很旧,因此不包含 C++11 库功能。

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 xcruntool.

如果您尚未安装 Xcode 的命令行工具,则可以使用该xcrun工具运行编译器和其他工具,而无需执行此操作。

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.

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

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

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

回答by Cornstalks

XCode uses clangand clang++when compiling, not g++(assuming you haven't customized things). Instead, try:

XCode 使用clangclang++编译时,不是g++(假设您还没有自定义东西)。相反,请尝试:

$ cat t.cpp
#include <iostream>

int main()
{
    int* p = nullptr;
    std::cout << p << std::endl;
}
$ clang++ -std=c++11 -stdlib=libc++ t.cpp
$ ./a.out 
0x0

Thanks to bames53's answer for pointing out that I had left out -stdlib=libc++.

感谢 bames53 的回答指出我遗漏了-stdlib=libc++.

If you want to use some GNU extensions (and also use C++11), you can use -std=gnu++11instead of -std=c++11, which will turn on C++11 mode and also keep GNU extensions enabled.

如果你想使用一些 GNU 扩展(也使用 C++11),你可以使用-std=gnu++11代替-std=c++11,这将打开 C++11 模式并保持启用 GNU 扩展。