Compiling C Programs on OS X 10.7+

One of the first posts I wrote for this blog, back in 2007, was about how to write and compile C programs using Mac OS X. That post was a result of me doing C programming in my first year of university, and since then plenty of things have changed. Not only have I finished university, but the basic procedure for installing Xcode has changed as well.

I thought I’d write a new tutorial updated for the more recent versions of OS X.

  1. Install Xcode from the Mac App Store
  2. Install the extra Command Line Tools from within Xcode by navigating to Xcode > Preferences > Downloads
  3. Create a new Xcode project by navigation to File > New > Project
  4. From the template list, under OS X, select Command Line Tool and choose Next
  5. Fill out the required forms, and under Type choose C
  6. Save the project to your computer
  7. Open main.c and write!

It’s possible that by default the toolbar won’t be shown to click Compile + Run, so you can press Cmd+R to do that directly.

The method in the original post continue to work, as long as Xcode installed as described above. Comments on that post provide additional information, like this one from mvdhoef:

instead of resorting to the default a.out you can use gcc the way it was meant to be used!!!
…………………….
gcc -o -Wall name file.c
…………………….
where name is the name of the program gcc will create,
file.c is the file and perhaps extension of the code you have created.

-o is to open new/rewrite in this case name (*if name already existed, it would overwrite without a second thought)
-Wall is another option which tells gcc to show all errors it encounters during compiling. (*this is optional)

Don’t forget to slack off while your code is compiling.

Posted in Macs Tagged with: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*