Programming in C on a Mac
To run programs written in C on the Mac, you have a couple of options. The
first is more involved than the second but allows you to run Microsoft
Visual Studio (the software we use in the lab) in a window on your Mac. You
therefore end up having to learn only one development environment. This is
the recommended option. The second is easier to get going but you end up
having to learn how to use a different development environment called XCode,
as well as learning how to use Visual Studio in the lab.
Option 1: Install Windows 7 and Visual Studio Express on your Mac
Start by downloading and installing VirtualBox from https://www.virtualbox.org/
- this software will allow you to install Windows as a "guest" operating
system on your Mac. When you're done, you'll be able to run Windows inside
a window on your Mac without having to reboot your machine.
Having installed VirtualBox, create your account on the DreamSpark website:
http://students.engineering.ubc.ca/free-software-students.
Please note that you will likely have to wait until the end of the second
week of term before your account can be created as UBC has to submit
registration information to Microsoft.
Add a copy of Microsoft Windows 7 Professional with Service Pack 1 (32
bit) to your cart and check it out. Follow the instructions on
screen and download the software.
When the download completes (it's a fairly large file), do the following:
1. Start VirtualBox
2. Click the "New" Button
3. Choose a name for this "virtual" machine - Windows7 would be a good
choice
4. Choose "Microsoft Windows" for the Operating System and "Windows 7" (not
"Windows 7 64 bit") for the Version, then click "Continue"
5. You must now choose the amount of memory that you will allow the virtual
machine to use. It is recommended that you select at least 2048 MB (4096 MB
is best) UNLESS this takes the slider into the red zone. If you end up in
the red zone, move the slider to the left so you use as much memory as
possible without going into the red zone.
6. In the next window you will create a virtual hard disk in which Windows 7
will be installed. Check "Create a virtual hard drive now" and click
"Create"
7. Select VDI (VirtualBox Disk Image), then "Continue"
8. Select "Dynamically allocated", then "Continue"
9. Enter "Win7" for the name of the VDI; for "Size" select at least 35GB
10. Click "Create"
11. In the VirtualBox main window, you will now see your new virtual machine
listed - select it and click "Start"
12. A "Select Start-up Disk" dialog will open in a new window.
13. Click the small folder icon and navigate to the .iso file that you
downloaded from DreamSpark
14. Follow the instructions on screen for completing the installation of
Windows 7
15. Once installation has finished, you should see Windows running inside a
window on your Mac!
Given that your new virtual machine has limited memory available to it and
limited disk space, it is recommended that you install Visual Studio 2012
Express edition. Note that you need to download it using a browser running
in Windows - Internet Explorer is installed by default. Instructions can be
found on the Visual Studio tab of the Course Outline on Connect.
Option 2: Install XCode
XCode is an alternative to Visual Studio and runs natively on a Mac. It is
available for download (free!) from the Mac App Store. Open up the App
Store, type XCode in the search box on the top-right corner and press
enter. A number of apps might be displayed, be sure to select the one
entitled "XCode" from the "Developer Tools" category. If you have a slow
Internet connection at home, you might want to do this while you're on
campus - the file that you need to download is about 1.5Gb in size! XCode
will install automatically into your Applications folder.
To compile and run a program in C using XCode:
- Start XCode!
- When the Welcome to XCode dialog box opens, click Create
a new XCode project
- You will then be asked to choose a template for your project - it is
very important that you get this right!
- In the left hand pane, under the OS X section, select Application
- In the right hand pane select Command Line Tool
- Click Next
- For the Product Name choose a name that describes the
application you are trying to write. It might be something like SphereVolume
or Oscillator.
- For Organization Name enter your name
- For Company Identifier enter your e-mail address backwards
but replace "@" with "."
So, for example, if your e-mail address is jo_gear@gmail.com
,
enter com.gmail.jo_gear
- In the Type drop-down list, select C
- Do not check Use Automatic Reference Counting
- Now select a folder on your hard disk where you'd like the project to
be stored
- Uncheck "Create local git repository for this project"
- Click Create
- In the left hand panel, you will see an icon labelled
main.c
- this represents your source file - click it to open it in an editor
window
- A template for your C program will be provided - a simple program that
prints
Hello World!
on the screen when it is run.
- You'll notice that the function
main
looks a little
different - feel free to replace int argc, const char * argv[]
with void
as we do in class
- Replace the code that prints
Hello World!
with your code
- To compile and run the program, select Run from the Product
menu.
- You will be asked if you want to enable developer mode on your machine
- click Enable and enter your password. You will have to do
this only once.
- The output will appear in a panel at the bottom of the screen.
- If you have any syntax errors (missing semi-colons, brackets in the
wrong place, identifiers not spelled correctly,...) an error message
will appear against a red background alongside the code.