A Quick and Easy Guide To Compiling C++ on Mac OS

Disclosure: Some of the links on this site are affiliate links. This means that, at zero cost to you, I will earn an affiliate commission if you click through the link and finalize a purchase.

How To Compile C++ On A Mac (A Quick And Easy Tutorial!)

This article will teach you how to compile your first C++ program using your Mac in just a couple of minutes, without any special tools!

But first I should share with you something quite surprising… I conducted a study and found that as many as 74% of new learners can’t complete the C++ hello world example!

The good news is I have created a resource to further aid those who are looking to learn C++ and best of all, it is totally FREE! Stick around until the end of the article to find out more.

C++ is a very versatile programming language. It’s used to make operating systems, browsers, and games, among other things.

C++ supports a variety of programming styles, including procedural, object-oriented, and functional programming. This makes C++ both powerful and adaptable.

If you are looking to develop apps for macOS then you may want to consider Swift instead, click here to find out just how easy it is to develop macOS apps with Xcode!

However if you are just looking to compile C++ on macOS, let’s jump in and get started!

Table of Contents

  1. Does C++ Work on M1 Mac?
  2. Does Mac OS Have A C++ Compiler?
    1. Is Gcc Installed on Mac?
  3. How Do I Compile and Run C++ on a Mac?
    1. How Do I Compile C++ in Mac Terminal?
      1. Step 1: Check the compiler version
      2. Step 2: Create a C++ file
      3. Step 3: Compile the code with G++
      4. Step 4: Executing the new program
    2. How Do I Use Xcode for C++ on Mac?
  4. Conclusion

Does C++ Work on M1 Mac?

Yes it is certainly possible to compile C++ on a newer M1 Mac which is using an ARM architecture processor.

I have authored this tutorial using an M1 Mac running MacOS Monterey 12.4, including examples and screenshots.

So I can confirm that there should be no issues completing any of these steps using an equivalent M1 Mac.

Does Mac OS Have A C++ Compiler?

Yes absolutely! Mac OS is a relative of Linux as both operating systems are derived from UNIX.

This means that Mac OS is compatible with the free GNU Compiler Collection, or GCC for short.

GCC was originally designed to be a compiler for the GNU operating system. The GNU system was created to be completely free software, in the sense that it respects the user’s autonomy.

Front ends for C, C++, Objective-C, Fortran, Ada, Go, and D, as well as libraries for these languages (libstdc++,…), are included in the GNU Compiler Collection.

Is Gcc Installed on Mac?

Gcc is not installed by default on Mac OS. If you try to execute commands such as gcc or g++ you will get the following error.

$ gcc
-bash: gcc: command not found

When you install Xcode, you will get all of the additional tools useful for developers. However this is one huge package and overkill if you just wish to use gcc from the command line.

You can get Gcc on Mac OS simply by installing something called Command Line Tools for Xcode.

Just open up a Terminal window and enter the following command.

xcode-select --install

This will cause a pop-up to appear giving you the option to install. The package is very small, just over 100MB so it shouldn’t take long!

How Do I Compile and Run C++ on a Mac?

I will assume that you now have either Xcode or Command Line Tools for Xcode installed, if not just have a quick read through the previous section.

The quickest and easiest way to compile a simple program is to do it directly in the Terminal. Later we can use the Xcode environment, which is beneficial for managing more complex projects.

How Do I Compile C++ in Terminal on a Mac?

In order to compile C++ using the terminal on a Mac, use the following steps:

  1. Open the Mac Terminal application from the applications > utilities folder, or search for it using Spotlight.
  2. First, check the compiler version.
  3. Create a new C++ file and write your code.
  4. Compile your code using G++.
  5. Execute your program.

Open the Mac terminal Application

In order to compile a simple C++ program in the terminal, first we will open a new terminal window. We will be using the g++ command, which is the part of GCC meant for compiling C++.

Step 1: Check the compiler version

First we can check the version of Apple clang with the following command.

g++ -v

The terminal should return some information about the version and target. If this information is returned then it confirms that the tools are installed correctly.

If you don’t get the correct output from this command, you probably need to install Command Line Tools for Xcode.

Step 2: Create a C++ file

Next we need to make a C++ file containing the code we wish to compile. For simplicity we will use the home directory but you can use any location that you prefer.

cd ~

Next we can create a new file and open it in the Nano text editor. This is a text editor that runs right in the terminal!

If you haven’t used text editors in terminal yet you might want to check out this article. Alternatively you can use a GUI text editor if you prefer, such as TextMate.

nano hello.cpp

We will use the file extension .cpp to indicate that it is C++ application source code.

When the editor opens, enter the following code. Once you have entered the code, save and close the file.

#include <iostream>

int main () {
        std::cout << "Hello World!";

        return 0;
}

Step 3: Compile the code with G++

Now it’s time for the fun part! First make sure you are still in the directory where you saved your hello.cpp file. Then you can use the g++ command to compile the code into an executable file.

We will first enter the g++ command, followed by the -o flag and then the name that we want to give the executable file, then followed by the name of the source code file we wish to compile, in this case hello.cpp.

g++ -o ./helloworld ./hello.cpp

This will create the file helloworld in the current directory. If you check in the GUI you should see both files there.

Step 4: Executing the new program

Congratulations! You just officially built your first executable program! You can double click it in the GUI in order to execute it, or enter the name in the command line.

./helloworld

On execution the program will print the text “hello world!” in the terminal.

So, whats next? Continue reading to learn how to compile C++ using Xcode. Or you can skip ahead to the next lesson.

How Do I Use Xcode for C++ on Mac?

It is also possible to build a simple C++ program like this in Xcode. In order to do so you will need to install the full version of Xcode, which is available from the app store.

Once Xcode has been installed, it can be found in the applications folder. When you run Xcode, you will be presented with a welcome screen.

Go ahead and click the option to Create a new Xcode project.

Next, select the Command Line Tool option under the macOS tab, then click next.

Fill out the following details with whatever you prefer. Choose C++ for the language.

Next select a folder where you wish to locate the project. In my example I have created a new folder on the desktop. I also opted to uncheck the create Git repository on my Mac as it is not required for this example.

This will create a new project and take you to the main Xcode screen. On the left-hand sidebar you will see a C main file has already been created.

If you click the C main file you should already see the hello world example code.

If you click and hold the play button above the left-hand sidebar and choose run, it will build and execute the code. You will see the output (the same as in our terminal example) in the console output at the bottom of the screen. Awesome!

Whats Next?

In this tutorial we have discovered how to quickly and easily compile and run a C++ program in MacOS.

The terminal is very quick and convenient and great for beginners when paired with TextMate, but Xcode will ultimately be a better option as projects become more complex.

Now that you have the compiler installed you can begin with the first lesson of C++ on MacOS, which expands on the hello world program and introduces variables.

Not in the mood for any more programming? Go ahead and see what other cool things you can do in the MacOS terminal instead, go ahead and take a look at my top 50 MacOS terminal commands!

Thanks so much for visiting my site! If this article helped you achieve your goal and you want to say thanks, you can now support my work by buying me a coffee. I promise I won't spend it on beer instead... 😏

2 thoughts on “How To Compile C++ On A Mac (A Quick And Easy Tutorial!)”

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top