c-cpp-setup

C/C++ Setup on macOS

This guide will help you install everything you need to write and run C/C++ programs on your Mac. We’ll use Apple’s built-in tools, so it’s completely free!

What We’re Installing and Why

Xcode Command Line Tools = Apple’s free C/C++ compiler and development tools. Think of it as the tool that turns your C/C++ code into programs that can actually run on your Mac.

Prerequisites (What You Need First)

Step 1: Open Terminal

Terminal is like Command Prompt on Windows - it lets you type commands to your computer.

  1. Find Terminal:
    • Press Cmd + Space to open Spotlight search
    • Type “Terminal” and press Enter
    • OR go to Applications → Utilities → Terminal
  2. What you’ll see:
    • A window with white or black background
    • Text that ends with a $ symbol - this is where you’ll type commands

Step 2: Install Xcode Command Line Tools

This is the main step that installs your C/C++ compiler.

  1. Type this command exactly:
    xcode-select --install
    
  2. Press Enter and follow the prompts:
    • A popup window will appear asking “Install developer tools?”
    • Click “Install” (NOT “Get Xcode” - that’s a much larger download)
    • Click “Agree” to accept the license
    • Wait for the download and installation (this can take 10-30 minutes depending on your internet)
  3. When it’s done:
    • You’ll see “The software was installed successfully”
    • Click “Done”

Step 3: Test Your Installation

Let’s make sure everything is working properly!

  1. Test the C compiler:
    • In Terminal, type: gcc --version
    • Press Enter
    • You should see something like:
      Apple clang version 13.1.6 (clang-1316.0.20.6)
      Target: x86_64-apple-darwin21.3.0
      Thread model: posix
      
  2. Test the C++ compiler:
    • Type: g++ --version
    • Press Enter
    • You should see similar version information
  3. If both commands show version information, congratulations! Your setup is complete!

What if Something Goes Wrong?

Problem: “command not found” when typing gcc

This means the installation didn’t complete properly. Here’s how to fix it:

  1. Check if tools are installed:
    xcode-select -p
    
    • If this shows a path like /Applications/Xcode.app/Contents/Developer or /Library/Developer/CommandLineTools, you’re good
    • If it shows an error, the tools aren’t installed properly
  2. Try installing again:
    xcode-select --install
    
  3. If it says “already installed” but gcc still doesn’t work:
    • Try installing the full Xcode from the App Store
    • Open App Store → Search for “Xcode” → Install Xcode
    • After installation, open Xcode once to complete setup

Problem: “xcode-select –install” says “already installed”

This means you already have the tools! Try verifying with gcc --version. If that doesn’t work, see the solution above.

Problem: Installation gets stuck or fails

  1. Check your internet connection - the download is large
  2. Free up disk space - you need at least 2 GB free
  3. Try again later - Apple’s servers can be busy
  4. Restart your Mac and try the installation again

Problem: Permission denied errors

Make sure you’re using an administrator account. If you still get errors, try:

sudo xcode-select --install

(You’ll need to enter your password)

Quick Summary

Here’s what you just accomplished:

  1. Opened Terminal on your Mac
  2. Installed Xcode Command Line Tools (your C/C++ compiler)
  3. Tested that everything works

What’s Next?

Now that you have a working C/C++ compiler:

  1. Install VSCode - Follow the VSCode Setup Guide to get a nice editor
  2. Write your first program - Try creating more C/C++ programs
  3. Practice - Get comfortable with compiling programs using gcc and g++

Understanding What You Installed

Important Note: On Mac, when you type gcc or g++, you’re actually using Apple’s clang compiler. It works exactly the same way as traditional gcc, so don’t worry about the difference!

Your Mac is now ready for C/C++ programming!


Troubleshooting Tips:


Back to: Home Next: VSCode Setup