Mac上使用C++ 11标准编译程序

《C++ Primer 5e》是基于C++ 11的标准写的,相比于之前的标准添加了许多新的特性,比如范围for循环。Mac上默认的C++编译器是Clang,我们可以在终端查看。

$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

下面我们写一段需要使用C++ 11特性的程序,这里就用范围for循环:

// range_for.cpp
#include 
#include 

using namespace std;

int main(int argc, char const *argv[])
{
    vector<int> v{1,2,3,4,5,6,7,8,9};
    for (auto &i : v)
        i *= i;
    for (auto &i : v)
        cout << i << " ";
    cout << endl;
    return 0;
}

直接编译g++ range_for.cpp会出现错误和警告

Mac上使用C++ 11标准编译程序_第1张图片

我们需要添加-std=c++11才能正常编译,
$ g++ -std=c++11 range_for.cpp
运行编译后的结果$ ./a.out可以得到正确的输出:
1 4 9 16 25 36 49 64 81

你可能感兴趣的:(c/c++)