← Back to list

A Quick Guide to Run C++ on a Mac

I have been coding in Python for over 20 years. All of a sudden, I need to run some C++ scripts on a Mac. I did have some experience with…

Manyi · 2026-04-11 05:39 · 2 claps · 1.9 min read paywalled
#cpp #cpp-programming #mac #clang #xcode
Open on Medium ↗
Wiki topics: 💻 · Programming 📱 · Mobile Development

A Quick Guide to Run C++ on a Mac

I have been coding in Python for over 20 years. All of a sudden, I need to run some C++ scripts on a Mac. I did have some experience with C/C++ but it was a while ago on Linux. To accomplish the task, I did some research on how to run C++ on MacOS, and it turns out to be quick and efficient. I am happy to share my experience here.

The Core: Understanding Clang

On macOS, your primary tool is Clang. While many systems use the older GCC (GNU Compiler Collection), Apple has standardized on Clang for several reasons:

  • LLVM Integration: Clang is the “front-end” for the LLVM compiler infrastructure. This allows for incredibly fast compilation.
  • Apple Silicon Optimization: Clang is built by Apple to squeeze every bit of performance out of the M-series chips. It understands the ARM64 architecture natively, ensuring your code utilizes the performance cores and unified memory efficiently.
  • Standards Support: Clang is often the first to implement the latest C++ standards (like C++23 and the upcoming C++26), making it a favorite for developers.

Setting Up the Environment

To get started, you just need the Command Line Tools (CLT).

Open your Terminal and run

xcode-select --install

This triggers a small download that provides the essential headers and compilers needed for development.

Once finished, check your compiler version by typing

clang++ --version

Here’s what I got

Apple clang version 21.0.0 (clang-2100.0.123.102)
Target: arm64-apple-darwin25.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Note: For advanced users or developers, you can consider Visual Studio Code or Xcode IDE.

Example Code (hello.cpp)

Let’s write a program that interacts with the user. This example uses standard I/O to take input and output a greeting.

#include <iostream>
#include <string>

int main() {
    int age;
    std::string name;

    std::cout << "Enter your name: ";
    std::cin >> name; // Takes a single word

    std::cout << "Enter your age: ";
    std::cin >> age;  // Takes an integer

    std::cout << "Hello " << name << ", you are " << age << " years old!" << std::endl;

    return 0;
}

The Command Line Tools make the “Write-Compile-Run” cycle very fast.

To compile, type in the terminal

clang++ hello.cpp

or

clang++ -std=c++20 hello.cpp -o hello

The first command outputs the default a.out . The option -std=c++20 tells Clang to use the 2020 standard features. The option -o hello names the output file "hello" instead of the default a.out.

To run the code, type ./a.out for the first case, and ./hello for the second case. You will be prompted to input!

Invalid non-standard header

The common non-standard header #include <bits/stdc++.h>, which includes every standard C++ library, is not included with Clang (default on macOS). To circumvent the issue, one can use the list of standard C++ libraries needed for the script. For example,

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

Using a smaller list of standard libraries significantly reduces compilation time.


메타데이터
post_id
e63f8019d274
slug
a-quick-guide-to-run-c-on-a-mac-e63f8019d274
url
https://medium.com/@manyi.yim/a-quick-guide-to-run-c-on-a-mac-e63f8019d274
canonical_url
https://medium.com/@manyi.yim/a-quick-guide-to-run-c-on-a-mac-e63f8019d274
author_url
https://medium.com/@manyi.yim
status
ok
fetched_at
2026-06-23 03:48:11