Codectionary / Developer documentation / C++

Install Apple Command Line Tools, check clang++, compile a first C++ file and resolve common macOS developer-tool issues.

Install or locate the command-line tools

Open Terminal and run xcode-select --install if the Apple developer tools are missing. macOS opens an installation dialog; accept the tools and wait for installation to finish. The package includes Apple’s compiler toolchain, macOS SDK, headers and related tools. If Xcode is already installed and selected, a separate Command Line Tools package is not required. The command may report that tools are already installed; in that case, move on to verification.

Check the active developer directory with xcode-select -p, then ask clang++ --version for the compiler version. xcrun --find clang++ shows the tool resolved by the active Apple developer directory. A result under /Library/Developer/CommandLineTools or an Xcode application can both be valid, depending on what you installed and selected.

Install only if developer tools are missing

xcode-select --install

Verify the active tools and compiler

xcode-select -p
xcrun --find clang++
clang++ --version

Create a .cpp file in your editor

Create a folder for the exercise, open it in your editor, and save the complete program below as hello.cpp. Check the filename carefully: it must end in .cpp, not .txt. The code prints Hello, Codectionary! to standard output and then exits successfully. The .cpp file is human-readable source; it must be compiled before it becomes a program that macOS can run.

If you use a terminal editor, you can create a folder with mkdir cpp-hello and enter it with cd cpp-hello. Creating the file in a graphical editor is equally valid. In the next step, make sure Terminal is in the folder containing hello.cpp before invoking clang++.

hello.cpp

#include <iostream>

int main() {
  std::cout << "Hello, Codectionary!" << '\n';
  return 0;
}

Compile and run with clang++

From the folder containing hello.cpp, run clang++ -std=c++17 -Wall -Wextra hello.cpp -o hello. The explicit standard mode avoids depending on a toolchain default; the warning options help catch common mistakes; -o names the executable. If the command succeeds, run ./hello. The leading ./ tells the shell to run the hello file in the current folder. You should see Hello, Codectionary!.

Edit the message, save the file, compile again and rerun. The executable does not update itself when you save source. A compiler error stops the new build; if an older hello still exists, it may still run, so do not mistake its old output for a successful rebuild.

In Terminal, from the folder containing hello.cpp

clang++ -std=c++17 -Wall -Wextra hello.cpp -o hello
./hello

Intel and Apple Silicon Macs

The same beginner steps work on supported Intel and Apple Silicon Macs. In Terminal, uname -m commonly reports x86_64 for an Intel environment or arm64 for a native Apple Silicon environment. The selected compiler and SDK normally build for the active host target unless you request a different architecture. Apple Silicon can also run an Intel-targeted terminal under Rosetta, so check the actual shell and build configuration if the architecture shown is surprising.

For this first local exercise, compile and run on the same Mac. Projects that distribute binaries to other Macs may need explicit target architectures, SDK choices and testing. Do not assume one executable automatically covers both architectures.

Inspect the active terminal architecture

uname -m

Common macOS setup problems

If clang++ is not found or macOS prompts to install tools, run xcode-select --install and complete Apple’s dialog. If xcode-select -p reports an invalid or missing developer directory after a macOS upgrade, check Software Update for compatible Command Line Tools or reinstall them using Apple’s documented process. If you have both Xcode and standalone tools, xcode-select -p and xcrun --find clang++ help identify which one is active.

If the compiler cannot find iostream or macOS SDK headers, verify the selected developer tools and their installation before adding random include paths. If ./hello says the file does not exist, check your current directory, the file name and whether compilation succeeded. If your editor saved hello.cpp.txt, rename it to hello.cpp and build again. A PATH that points to a different third-party clang++ can change which compiler runs; inspect clang++ --version and xcrun --find clang++ before troubleshooting its flags.

Syntax

#include <iostream>

int main() {
  std::cout << "Hello, Codectionary!" << '\n';
  return 0;
}

Examples

First macOS C++ terminal program

Save this as hello.cpp, build it with Apple clang++ and run ./hello in Terminal.

#include <iostream>

int main() {
  std::cout << "Hello, Codectionary!" << '\n';
  return 0;
}
  1. This complete program verifies that the selected Apple developer tools can compile and run a simple C++ console application.
  2. #include <iostream> makes standard output available. main is the entry function; the braces contain the statements that run.
  3. std::cout prints the message, the newline moves the next output to a new line, and return 0 signals normal completion.
  4. The source file is not itself an executable. Save it, compile with clang++ and only then run ./hello from the folder containing the built file.

Best practices

  • Use Apple Command Line Tools for a first terminal program; install the full Xcode IDE only when a project needs it.
  • Check the selected developer directory and clang++ version before assuming which compiler is active.
  • Recompile after every source change and verify the build succeeded before running the executable.

Runtime & versions

Compiled native applications. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

ISO C++ resources

Specifications & further reading