How to enable C++17 on Mac?
Asked Answered
R

6

40

I am able to update gcc on Linux to get -std=c++17, but cannot do the same on Mac.
Is there a version of Clang I can update to or some other alternative to get C++ 17 on my Mac?

Please help.
Thanks.

Relume answered 23/8, 2016 at 1:55 Comment(1)
You can also have gcc on Mac.Knickers
P
28

On my 10.11 El Capitan, Xcode 7.3.1, clang has been updated to:

Apple LLVM version 7.3.0 (clang-703.0.31)

which is almost equivalent to llvm version 3.8. clang++ hasn't -std=c++17 option, but -std=c++1z, working well at present, though only supporting some features of C++1z.

For gcc, you can install a very new one by:

brew install gcc --HEAD

which will install gcc-6.1 now, (2016.8). This gcc has limited support for C++17 and can be enabled by -std=c++17.


some update:

The corresponding llvm version of Apple's clang is not clear recently, should be 3.9+ or 4+. Check this wiki page for more information.

brew install gcc --HEAD should always work for mac/homebrew, and give you the cutting edge gnu gcc with many experimental features. On the other hand, normal brew install gcc should install a gcc supporting most c++17 features now.

For the language standard argument, try -std=c++17 and -std=c++1z for different compilers, one of them should work.

Pancreatin answered 23/8, 2016 at 4:7 Comment(1)
Thank you. I tried and worked. had to make CXX=g++- instead of g++ on the Darwin build.Relume
P
10

Apple's Clang already supports up to the latest C++ version but is defaulted to older ones.

All you have to do is add --std=c++17 after g++ while execution, execute your code with g++ --std=c++17 <filename>.cpp

If you're using code runner for execution, follow:

  1. Code>Preferences>Settings in search bar at top of settings type code-runner.executormap, enter image description here

  2. Select Edit in settings.json, A Json file file open enter image description here

  3. from thissettings.json file change the line with key cpp line:8 in following example "cpp": "cd $dir && g++ --std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",

4. Restart VSCode

Proparoxytone answered 5/8, 2021 at 4:52 Comment(0)
M
6

The v17 standard is now offered by default with Xcode 10, offering both standard, and variant flavors.

Via Build Settings:

CLANG_CXX_LANGUAGE_STANDARD : C++17/GNU++17

CLI:

-std=c++17 partial support
-std=gnu++17

Methadone answered 12/9, 2019 at 19:43 Comment(2)
Note that (IIUC) C++17 features are still partially supported.Merrymaker
Thanks @FranklinYu! I updated the post to point out partial support for the Clang implementation.Methadone
A
2

You can install gcc from sources (it's not complex at all) if there are some complications with the repositories.

Installation process for mac os X, and here it is for linux. Hope it helps.

Auberbach answered 23/8, 2016 at 2:10 Comment(0)
S
1

You need to first install a compiler that supports the C++17 standard. I can only recommend installing gcc, clang, and Apple's LLVM:

  • gcc 9 or 10
  • clang 9 or 10
  • XCode LLVM 9.3 or later

Others such as, PGI, Intel Compiler, etc. might work, but I have no experience with those.

gcc and clang can be installed using homebrew

gcc:

brew install gcc

clang

brew install llvm

You can get XCode through the App Store.


On a Mac, the C++17 Parallel STL requires Intel Threading Building Blocks (TBB).

The easiest way to install tbb is through homebrew:

brew install tbb

When compiling from the command line using gcc or clang don't forget to include the flag: -std=c++17

Sewing answered 26/7, 2020 at 16:19 Comment(0)
L
-6

Maybe you should install xcode command tool using:

xcode-select --install
Langobardic answered 13/4, 2019 at 2:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.