MacOS Native VS Code for Programming C++
Here is how you can write C++ programs on your own Mac by installing all the programs on your computer. This will allow you to write C++ programs without connecting to an external server (no internet required); however, it requires a few more install steps than other methods.
Best Mac Option: VS Code
To run VS Code on your Mac, you'll need to install some tools and then use the provided starting project for MacOS to help it run correctly. The steps below come from the setting up VS Code under MacOS: Guide from Microsoft
Install VS Code and Tools
- Install VS Code on your MacOS device.
- Install the "C++ extension for VS Code".
- Install the C++ compile (
clang
):- From a MacOS terminal, run
clang --version
- If this does not work, then you'll need to install Clang via the
xcode-select
tool from the terminal:
xcode-select --install
- From a MacOS terminal, run
- Download this starting C++ project for MacOS..
- Extract the .zip file into a new folder name
CMPT130
in a location where you want to store your code, such as a documents folder or your desktop. - Inside your
CMPT130
folder there will now be a.vscode/
folder which contains the configuration files needed to make development easy. You may not be able to see this.vscode/
folder in the file finder because anything starting with a.
may be hidden by default. - Inside this
CMPT130
folder, you will create a new folder for each section of notes, each lab, and each assignment.
- Extract the .zip file into a new folder name
Each time you run VS Code, you'll need to do the following:
- Open VS Code. If needed use File > Open Folder to open the
CMPT130
folder you created above (from the starting project for MacOS). - If needed, in the folder view on the left, create a new folder for your activity (such as
lab1
orasn2
; don't put spaces in these names). - Open (or create) a .cpp file in your folder (such as
lab1/lab1.cpp
oras2/calculator.cpp
). - In the top-right of the .cpp file, click the play button.
- It should open a new terminal (or two) and show you the output of your program.
- VS Code should now work with debugging the program as well.
Troubleshooting
- Ensure your C++ file and folders are named without any spaces in the file name.
- Ensure your C++ file has a name ending in .cpp, such as
lab1.cpp
- If you run your .cpp file and the output of your program (
cout
s) shows up inside the debugger output (and makescin
statements unusable because it's connected to the debugger), then try downloading and using the Mac starting project again. It is likely some of the settings inside the.vscode/
folder are incorrect on your computer.
Submitting Code from MacOS Native
With a native install on MacOS, all your code is stored on your personal computer in the cmpt130
folder that you created to start with. Each time you launch VS Code you'll need to re-open that folder (if not done automatically).
To submit your C++ code (.cpp files) for labs or assignments, use your normal web browser and upload your files from the cmpt130
folder.
Note: Since the files are on your home computer, it's essential that you backup your files. I recommend having an automatic backup program for these files. Almost every student experiences a file loss some time throughout their degree (I did, twice!); have a backup!
Running a program using the command line
If you are unable to run your program through your IDE, you can run it through the command line (terminal).
Each time you change your .cpp file, you'll need to do:
- Compile the .cpp file:
Go to Terminal menu > Run Build Task. Hotkey is Shift-Command-B - Press any key in the compiler output terminal to close that terminal.
- Change to the folder inside your CMPT130 folder for your current file (for example, a
lab1
folder):
cd lab1
- Run the program via the terminal you opened. Assuming your file is
myprogram.cpp
, then run:
./myprogram
- Interact with your program via the terminal.
Hint: you can repeat a previous command in the terminal by pressing the up arrow on the keyboard.
Note: This does not support running your program with the integrated debugger. If you try to use Run > Start With Debugging, or Run > Start without Debugging, it will run your program inside the debugger; however, it may not let you send data to your program from the keyboard. Hence, cin
statements won't work!
You may have some success using the "Code Runner" VS Code extension to run your code. However, it has been known to not work for some students (2023).
Alternate Good Mac Option: XCode
XCode is the full (professional, but free!) integrated development environment for Mac. It is quite different that VS Code, but would be perfectly reasonable to use to complete all assignments for this course.
- If you are running the latest version of MacOS, you can find the most recent version of XCode on the Mac App Store.
- If you are running an older version of MacOS, you can download an older version of XCode from the Apple Downloads page.
- See the XCode requirements page to find out the version of XCode you need for your version of MacOS.
- You will need an Apple ID to download from the downloads page. It is free to create an account.
- To use XCode, you may need to follow some tutorials, such as this WikiHow XCode tutorial.
- When you create an XCode project, you likely want to create a Command Line Tool.
- You can type in any name you like for the organization.
- When you submit your assignment, be careful that you are submitting a .cpp file. Sometimes XCode likes to store projects as a file. Double check your file is plain code by first submitting it to CourSys, then opening it up from CourSys to see if it's just C++ code.
VS Code in a Dev Container - May be a little slower
If XCode isn't going to work and you want to run VS Code, you can install a Dev Container. You could also use a virtual machine under MacOS (such as Parallels Desktop, or VMWare Fusion) and then install Linux into that VM. These options may be a little slower than just running VS Code or XCode under MacOS, but they gives you the power to run other software.