C++11 in Mac (VsCode)

For some purposes, mac is using clang instead of gcc. You can still use clang++ or g++ to run a .cpp file. But basically, clang will use -std=c++98 as default.

So if you want to switch to new c++ standard in VsCode (Mac), do as follows:

  1. Edit you c++ settings of VsCode:

    1. create .vscode/c_cpp_properties.json

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      {
      "configurations": [
      {
      "name": "Mac",
      "includePath": [
      "${workspaceFolder}/**"
      ],
      "defines": [],
      "macFrameworkPath": [
      "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
      ],
      "compilerPath": "/usr/bin/clang",
      "cStandard": "c11",
      "cppStandard": "c++11",
      "intelliSenseMode": "macos-clang-x64"
      }
      ],
      "version": 4
      }
    2. In terminal:

      1
      2
      echo "alias g++='g++ -std=c++11'" >> ~/.bashrc
      source ~/.bashrc

Run your .cpp files in terminals:

1
2
g++ filename.cpp -o objectname
./objectname

Note: you can use g++ in std11, but clang++ is still in std98. (clang/gcc is for .c file)