5 Ways to Write C++ Code on Your Mac

7 minute read
| Analysis

This article is intended primarily for students leaning C++ for the first time on a Mac. It’s not a step-by-step tutorial on how to write and compile code in the applications described. That will require digging in and mastering the mechanics of each method described. Rather, it’s a quick start guide that points to the basics and provides simple examples of how C++ coding can be accomplished on a Mac.

2017 imacs

Yes, there are lots of ways to write C++ on a Mac

 

 

Scope of Article

All of the solutions provided can be used in the writing of the kinds of code beginners write: 20 to 50 lines of code that assist with the learning of C++. Stdin and stdout are the focus. Others can do much more. The writing of a full-featured GUI app for macOS is not covered.

The article could also be used by professionals who’ve already been doing professional C++ on, say, a corporate Linux system. However, it may not be clear how to get started and which tools to use in a transition to a Mac.

This article provides an overview of five good ways to write C++ on a Mac. There may be more. Most discussed are free, but one incurs a modest cost. Some are full featured, and some have limitations. For example some solutions presented don’t lend themselves to debugging thanks to decisions Apple has made. For all these reasons, it’s hard to rank the various solutions by some standard. After you’ve read about them, it may well be that one is the obvious way to proceed initially. See the spreadsheet on page 6.

First, a bit of nomenclature. We’ll talk here about an Integrated Development Environment (IDE). From Techtarget:

An integrated development environment (IDE) is a software suite that consolidates the basic tools developers need to write and test software. Typically, an IDE contains a code editor, a compiler or interpreter and a debugger that the developer accesses through a single graphical user interface (GUI).

What follows is a thumbnail sketch of each approach. As always, it’s up to the student to install and learn the nuances of each one. We’ll provide merely a grand tour and some overall guidance.

Finally, in the screen shots below, for clarity, the important area only is shown. That’s because showing the entire window often makes the key region too small to read.

1. Apple’s Xcode IDE

As mentioned above, Apple’s Xcode is a free, full featured IDE for native apps. However, it’s not very hard to press it into service for beginning C++ code on a Mac, and it includes a GUI debugger.

Note that Xcode is designed specifically for developers to write macOS, iOS, tvOS and watchOS applications in Swift or Objective-C. Because of that, it’s not widely used in industry or education as a C++ development system. In those situations, the focus is on Linux and Java-based IDEs. But Xcode is free, powerful and turns out to be amazingly useful for the student with a Mac.

All you need is a (free) AppleID and then you can download it from the Mac App Store found in macOS: Apple Menu > App Store. The latest version as of this writing is 8.3.3 and is designed to work with macOS Sierra.

Xcode in Mac App Store

Xcode in Mac App Store

1.1 After you install Xcode, there will be an app in /Applications called Xcode.app. When you lunch it, you’ll see:

Welcome to Xcode.

1.2. Select “Create a new Xcode Project.” Then click on “Command Line Tool.”

Xcode Command Line Tool

Select the “Command Line Tool.”

1.3. Fill in the various fields, as you wish, and select C++ from the Language popup.

Xcode Language.

1.4. At this point, you’ll need to learn about how to enter code into the editor, build (compile and link), display the stdin/stdout and debug windows, and run the app. Here’s how it looks with some sample C++ code designed to calculate the area of a rectangle..

C++ in Xcode

C++ in Xcode with input and output (bottom right).

A final note. You’ll be able to create breakpoints and display variables in Xcode like this:

Xcode debug with breakpoint.

Xcode debug with breakpoint and variables shown.

For the beginning C++ student who is familiar with the Mac, this is a great way to go. But there are more options, so off we go.

Next: Method #2 – Install Linux as a Virtual Machine in macOS.

4 Comments Add a comment

  1. beachcomber

    Qt Creator IDE was written by C/C++ programmers for C/C++ programming.
    Wikipedia: Qt Creator
    Qt Creator

    Dual license: Free & Open Source and Commercial.
    Qt Downloads

    Plain vanilla C/C++ code can be written without using the Qt Cross Platform Framework libraries. (Yet, with Qt Framework, one can also write C/C++ apps for iOS and Android on macOS.)

    For me, Qt Creator has been an nice replacement for Lightspeed C (aka THINK C). 😉




    0
  2. leeeoooooo

    OF COURSE you can use Xcode to code and compile C++. That’s how the engineers at Apple do it!

    Chris Lattner and his compiler team were well known to be big fans of C++.

    Many of the low-level libraries in OS X, especially audio and I/O are written in C and C++.

    They discourage third party application programmers from using it because of all the sharp edges and dangerous curves, but they use it plenty themselves.




    0
Add a Comment

Log in to comment (TMO, Twitter, Facebook) or Register for a TMO Account