Skip to content

Instantly share code, notes, and snippets.

@danielledeleo
Last active May 20, 2022 11:54
Show Gist options
  • Save danielledeleo/772f059aa8d245888a8a to your computer and use it in GitHub Desktop.
Save danielledeleo/772f059aa8d245888a8a to your computer and use it in GitHub Desktop.
Compiling and Running C on Mac OS X 10.10 Yosemite

Compiling and Running C on Mac OS X Yosemite

If you're like me, you find working on the provided Ubuntu VM to be tortuous and inefficient. It's such a shame to be running an operating system with all of the tools to you need to compile and run simple toy C programs but being forced to use a clunky virtual machine without all your favourite software. Here's what I do to ease the pain.

Mac OS X

This guide was tested on Mac OS X 10.10 Yosemite, but should also work on any version of OS X that Homebrew supports. It should work just fine on Mavericks, plus you get access to valgrind. (valgrind hasn't been ported to 10.10 yet, but it does work on 10.9)

gcc on OS X

By default, if you have Xcode installed, typing gcc into Terminal.app links to the llvm/clang compiler. Although clang is a great, modern compiler it isn't what's specified by the professor and could potentially lead to incompatible code (and lost marks). To avoid this, we can install and run the same (*a slightly newer) version of gcc natively on our Macs.

Homebrew

If you don't already have Homebrew installed, go ahead and do that now. For those of you familiar with Linux package managers, Homebrew is essentially Aptitude (apt-get) for OS X.

From brew.sh:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

The installer will guide you through installing any missing dependencies you may have. (Commonly Xcode tools)

Once Homebrew is installed, brew install gcc

And that's pretty much it. But you have to watch out; gcc is actually just clang plus some extra libraries, you need to run gcc-4.9 instead.

Notice how gcc -v spits out something like

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix

Which, clearly, isn't what we want. Notice the slight difference between clang -v and gcc -v.

Whereas gcc-4.9 -v should show something like

Using built-in specs.
COLLECT_GCC=gcc-4.9
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/4.9.2/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/lto-wrapper
Target: x86_64-apple-darwin14.0.0
Configured with: ../configure --build=x86_64-apple-darwin14.0.0 --prefix=/usr/local/Cellar/gcc/4.9.2 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-4.9 [omitted]
Thread model: posix
gcc version 4.9.2 (Homebrew gcc 4.9.2) 

Note: if gcc-4.9 isn't being found, try opening a new shell. The $PATH sources need to be refreshed.

It's probably a bad idea to alias gcc to gcc-4.9 because some programs (Homebrew included) may expect clang. Instead, I suggest using Makefiles to easily specify your compiler.

ALWAYS test your code in the provided VM before submitting! You have been warned.

Using llvm/clang

Another option is just to use what's included with your system. llvm/clang is a very capable, modern compiler and can, for the most part, compile the exact same code that works in gcc, at least in the scope of COMP 2401.

Caveats

gcc (clang) appears to compile everything as C++ code, so you'll probably get some weird warnings that you otherwise wouldn't have seen. Fortunately, I've found that fixing things for clang tends to keep gcc-4.9 happy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment