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

Published Categorized as Command Line, DIY Compiling, Mac, Programming
Quick and Easy Guide To Compiling C++ on Mac OS

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!

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.

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 Mac Terminal?

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 Atom.

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.

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!

Conclusion

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, but Xcode will ultimately be a better option as projects become more complex.

To see what other cool things you can do in the terminal, go ahead and take a look here!