Development

How to install C++ 17 on macOS

Captain Salem 3 min read

How to install C++ 17 on macOS

C++17, also known as “ISO/IEC 14882:2017,” is a version of the C++ programming language. It is the successor to C++14 and was standardized by the International Organization for Standardization (ISO) in 2017.

C++17 introduces several new features and improvements to the language, aiming to make C++ code more expressive, concise, and efficient. Some of the key additions and enhancements in C++17 include:

  1. Structured Bindings - Allows unpacking of structured types (such as tuples or arrays) into individual variables using a more concise syntax.
  2. if/switch with Initialization - Allows variables to be defined and initialized within the condition of if and switch statements.
  3. constexpr if - Introduces compile-time conditional statements, enabling selective compilation of code based on constexpr conditions.
  4. Inline Variables - Enables the definition of variables inside a class definition, making it easier to define constants or small utility objects.
  5. Range-based for loop with initializer - Allows the initialization of the loop variable directly within the range-based for loop.
  6. Fold Expressions - Simplifies the handling of variadic templates by providing a concise syntax for operations such as folding a parameter pack with a binary operator.
  7. Structured Attributes - Adds attributes to enable structured bindings and other constructs to be used with standard attributes like nodiscard and fallthrough.
  8. Parallel Algorithms - Introduces standard parallel algorithms to leverage multi-core processors efficiently.
  9. Filesystem Library - Provides a standardized library for file system operations, including file and directory manipulation, path handling, and file metadata.
  10. Standardized Attributes - Adds new standard attributes, like maybe_unused, nodiscard, and fallthrough, to improve code clarity and maintainability.

These are just a few of the new features and enhancements introduced in C++17. The goal of C++17 was to improve developer productivity, enhance the language capabilities, and provide more straightforward and safer ways to write C++ code.

Install C++ 17 on macOS

To get C++ 17 running on macOS, we need to ensure that we have a C++ compiler that supports C++ 17 and the required developement tools.

Luckily, we can use the Clang compiler which natively supports C++ 17.

Step 1 - Install Xcode

It will come as to no surprise that to do any development with a low-level language such as C or C++, you will need to have the Xcode tools installed.

You can check if you already have it installed by running the command:

Open a terminal window and run the command below:

xcode-select --version

This command should return the installed version of xcode as shown in the output below:

xcode-select version 2405.

If you do not have Xcode installed, we can install it by running the command:

xcode-select --install

Once you have xcode installed, we can proceed and install Clang.

Step 2 - Install or Update Homebrew

The next stepm, you need to ensure you have Homebrew package manager installed. We have a dedicated tutorial on that so check it out below.

https://www.geekbits.io/how-to-install-and-use-homebrew-on-macos/

Once installed, run the command below to update.

brew update

Step 3 - Install the Clang compiler with C++17 support:

Using Homebrew run the command below to install the Clang compiler:

brew install llvm
CleanShot 2024-02-05 at 11.58.06@2x
CleanShot 2024-02-05 at 11.58.06@2x

This should download and install the latest version of Clang which comes with native support for C++ 17.

Step 4 - Set up Env Variables

To efficiently work with Clang and the C++ development tools, we need to configure a few environment variables.

We can do this by editing the shell profile. Either usign the .bashrc or .zshrc.

Add the following lines to your profile file:

echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.zshrc
export LDFLAGS="-L/opt/homebrew/opt/llvm/lib"
export CPPFLAGS="-I/opt/homebrew/opt/llvm/include"

Save the file and reload the changes, you can do this by running the source command as:

source ~/.zshrc

Replace the .zshrc with .bashrc if you are using the Bash shell.

Step 5 - Verify the installation:

Once everything is setup, run the following command to check if Clang with C++17 support ready.

clang++ --version

Output:

Apple clang version 15.0.0 (clang-1500.1.0.2.5)
Target: arm64-apple-darwin23.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Once you have written your C++ 17 code, you can compile it with C++ 17 using the command:

clang++ --std=c++17 {filename}.cpp

Replace the filename.cpp with the actual name of the file you wish to compile.

Using C++ On GNU Compiler

If you are instead using the G++ compiler for CPP, you can run the code with CPP 17 by running the command shown below:

g++ --std=gnu++17 {filename}.cpp

Note: It is good to ensure that the GNU compiler you are using supports C++ 17.

Conclusion

In this tutorial, we quickly covered how to setup and use the C++ 17 compiler features on macOS. We learned how to work with Clang and GNU C++ compiler.

:) Later!

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.