OSX Set Up for C++
Guide to setting up OSX operating systems for C++ development.
The following is a guide on getting your Mac set up for coding. There are a couple of ways to code on OSX, here we will show you one of the ways that works with the software that the school uses.
DISCLAIMER: The University of Lethbridge is not responsible for the setup of your machine. This guide has been created by the ULCSC. The information in this tutorial should be correct for the most part, however you should still proceed with with discretion.
Configuring Shell
Zsh or Z shell is the preffered shell to use on MacOS. Of course, for this tutorials purposes bash should still work. If you choose to use something even more exotic like fish, or ksh I wish you the best of luck. For the this tutorial I’ll be using zsh.
Check if Zsh is Installed
1
2
3
4
5
# This command will work:
which zsh
# This command will also work:
echo ${SHELL}
Your terminal should output something like /bin/zsh
.
Enable Zsh
Zsh should be enabled by default, however if it is not, enable it by running:
1
chsh -s $(which zsh)
If the command above didn’t work, or you got a system message saying you need to install dev tools, install them. Install with:
xcode-select --install
That may take a bit of time, once it finishes, switch to zsh.
Alternatively, you can open up your terminals prefferences by running cmd ,
(not in the terminal). and changing the radio button labeled “shells open with” from default to command and entering ‘/bin/zsh’.
For the switch to take effect, you may have to quit and shut down terminal.
Install XCode
You can use this command to install XCode: xcode-select --install
. It is very important to note, the next part is non essential. If you have developer tools installed, you should already be able to compile most projects with clang. For most of your degree, that is all you will need. This point forward is optional, though, probably not a bad idea to insall Homebrew.
Install Homebrew
Homebrew is a package manager that simplifies downloading and installing software on MacOS.
Run the following command in your terminal:
1
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
Enter Zshrc
Now go to your home directory, you can reach it by typing cd
. Then type ls -a
this should show you all of the files and directories in your home directory (even the hidden ones). We are looking for the .zshrc file.
It should be there by default, however if its not there run the following:
1
touch ~/.zshrc
You can open your zshrc using any editor. You can use any editor, for this you may have the easiest time using vim, nano or if you’ve configured it, vs-code with code filename.filetype
. This way you can open up the .zshrc in terminal.
It is highly recommended that you watch a tutorial on using vim / nano before using them, as writing to, saving, and quiting in the editors is a little finicky. On OSX, you can also learn by typing
vimtutor
in the command line.
Format Zshrc
The zshrc allows you to configure what your shell can do. In the next step we will create an alias that will allow you to use the g++ compiler, but there are a couple of things we must do before reaching that step.
First of all, type brew doctor
into the terminal, if you don’t get an error, skip this step entirely.
Add Homebrew to path
The only formatting that you might have to do right now is adding Homebrew to your path. This will allow you to access what you have installed with Homebrew in your terminal.
First of all, type cat .zprofile
in your terminal, if you have something similar to:
1
2
3
4
5
6
7
8
# On Apple Silicon:
eval "$(/opt/homebrew/bin/brew shellenv)"
# Intell:
eval "$(/usr/local/bin/brew shellenv)"
If you don’t, don’t worry, you can use your zshrc:
1
2
3
4
5
6
7
8
# For Apple Silicon add the following to your .zshrc:
export PATH="/opt/homebrew/bin:$PATH"
# For Intell, add the following to your .zshrc:
export PATH="/usr/local/bin:$PATH"
Where usr is the name of the user (can use whoami
terminal command to find out usr).
Install gcc / g++
Using Homebrew we can now install gcc and g++.
1
brew install gcc
Add g++ to zshrc
Now to compile files with g++, you need to create a terminal alias for g++, there are a couple of ways to do this, however the easiest is to create an alias in you zshrc.
Write all aliases after Exports
Apple Silicon
1
alias cxx="/opt/homebrew/opt/gcc@14/bin/g++-14"
Intell
1
2
# just updated my system to us @14, not sure if it will work on intell macs though
alias cxx="/usr/local/opt/gcc/bin/g++-14"
Now to run c++ files in your terminal you can run
1
2
3
g++ -o exe test.cpp # To compile the file
./exe # To run the executable
rm exe # To remove the executable
A Note on Makefiles
If you start using makefiles, and don’t want to use clang, you must add the path to the g++ in homebrew to the make file. If you don’t it will default to using clang.
Issues / Feedback / Contributing
If you see any problems with these pages (incorrect information, misspelled words, incorrect formatting), please create an issue on the repo, or let one of the executives know.
If you have an idea for a resource or page that could be useful, please make a pull request so it can be added to the site, see more detail in the README.