Search Authority

How to Install g++ on Mac: Step-by-Step Guide

Getting a modern C++ toolchain on Apple hardware is straightforward if you know where to look. This guide focuses on installing g++ on macOS with clear steps and diagnostic checks.

Mara Ellison
How to Install g++ on Mac: Step-by-Step Guide

Getting a modern C++ toolchain on Apple hardware is straightforward if you know where to look. This guide focuses on installing g++ on macOS with clear steps and diagnostic checks.

You will set up a reliable compiler for command-line builds and integration with editors, while avoiding common permission and path issues.

Component Purpose Default on macOS Recommended Source
clang Default system compiler Yes Xcode Command Line Tools
g++ GNU C++ compiler (GCC frontend) No Homebrew gcc formula
make Build automation Limited (BSD make) GNU make via Homebrew
cmake Cross-platform build system No Homebrew cmake

Install Command Line Tools

Verify Xcode Compatibility

macOS does not include g++ by default, but it does include clang. Install Xcode Command Line Tools first, as they provide compilers, headers, and SDKs needed by GCC.

Terminal Execution

Run xcode-select --install in Terminal and follow the graphical prompt. This action places essential development utilities in /usr/bin and sets up the active developer directory.

Install Homebrew Package Manager

Why Use Homebrew

Homebrew provides up-to-date GCC packages on macOS without interfering with system tools. It installs to /usr/local or /opt/homebrew on Apple Silicon, keeping binaries isolated and manageable.

Installation Command

Execute the official install script /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" in Terminal. Add Homebrew to your PATH as instructed, and link it early in your shell profile.

Install g++ via GCC Formula

Formula Details

The Homebrew gcc formula installs GNU Compiler Collection, which includes g++ named gcc-{version}. On Intel Macs it typically appears as g++-13; on Apple Silicon it follows the same pattern.

Linking and Version Selection

After installation, create a safe link like g++-local pointing to the versioned binary. Use update-alternatives or a manual symlink in ~/bin to choose a consistent g++ invocation without modifying system directories.

Verify and Troubleshoot Setup

Basic Validation

Confirm installation with g++ --version and verify include paths using g++ -v -E -x c++ -.

Common Issues

Permission errors may require sudo chown on certain directories. PATH order determines which g++ is used; ensure your shell profile places user-local bin directories before system paths when needed.

Final Setup Recommendations

  • Install Xcode Command Line Tools before Homebrew
  • Use Homebrew to install the latest gcc formula
  • Create a user-local bin directory and link g++-local
  • Adjust PATH so user binaries take precedence when needed
  • Verify with g++ --version and test a simple program

FAQ

Reader questions

Why does g++ appear with a version suffix instead of just g++?

Homebrew installs versioned GCC binaries to avoid conflicts with system tools, so you use g++-13 and update alternatives to set the default.

Can I install g++ without Homebrew on macOS?

Yes, you can build GCC from source or use MacPorts, but Homebrew offers automated dependency handling and easier updates for most users.

Will installing g++ interfere with Xcode development?

No, because Homebrew keeps GCC separate from the system clang toolchain; Xcode projects continue using their configured compilers unless explicitly switched.

How do I ensure make uses the correct compiler?

Set CC and CXX environment variables to point to your selected gcc and g++ binaries, or configure them in your shell profile to persist across sessions.

Related Reading

More pages in this topic cluster.

Who Designed the Nike Logo? The Story Behind the Swoosh

The Nike swoosh is one of the most recognizable symbols in the world, but few people know the story behind its creation. This piece explores who designed the Nike logo, why it h...

Read next
What is the World's Hottest Pepper? 🌶️🔥

When people ask about the world's hottest pepper, they usually mean the variety that currently holds the Guinness World Record and pushes the boundaries of capsaicin heat. Peppe...

Read next
Jon Huertas in This Is Us:角色, 出演时期与剧情影响详解

Jon Huertas 在《这就是我们》中饰演成年 Kevin Pearson,这一角色从2016年首播持续至2022年最终季,构成了剧集核心家庭叙事的重要组成部�...

Read next