Write C/C++ on Mac with Eclipse, no Xcode

| How-To

Apple’s Xcode development system is superb for developing applications, but sometimes you just want to write C or C++ code for research or school. Composing a serious chunk of code with vi is no longer acceptable,* so users in this frame of mind are now using Eclipse, a modern IDE, that’s also free. Here’s how to get gcc without installing Apple’s Xcode and then install Eclipse for C/C++ programming.

What’s the Motivation?

At work recently, my wife was chatting with a colleague who was taking his first C++ class. She taught him how to use Eclipse on a Mac, even though he resisted at first. However, later, he came back and commented that the other students were trying to manage ever increasingly complex projects with the vi editor. It was taking them four, six, or even 20 hours in some cases to complete their homework each week. He finished his, typically, in 30 minutes. That’s the power of an IDE with a modern debugger. That last item, the debugger, can’t be emphasized enough. Print statements in your code are oh, so yesteryear with a tool like this. Time is money, and efficiency reflects on you as a programmer.

So if you’re a scientist, researcher or engineer who wants to write some research code, not intended as a GUI app, in Java, C, C++ or Fortran, you need to dump vi as an editor* (or Emacs or Nedit or whatever) immediately and get with this kind of IDE. Things are moving far too fast nowadays not to make this important move. (Clearly, I’m speaking to an older crowd here.**)

To be perfectly clear, Apple’s Xcode is a fabulous development system for C, C++, Objective-C and even Fortran 77***. You can build native OS X and iOS apps. But many researchers and scientists aren’t interested in Xcode. They’ve come from a Linux or other UNIX platform, like IBM’s AIX, and they just want to carry on their research in Eclipse on a Mac. This how-to is primarily for them. But, as I mentioned above, students who are taking their first programming class and own a Mac will also find this discussion useful — indeed mandatory. Remember, this is an introduction to whet your appetite and get you launched, not a complete Eclipse tutorial.

Also, this how-to for the sake of simplicity focuses on C/C++, but Eclipse can handle a myriad of languages, including, but not limited to, Java and Fortran. Let’s start with C/C++.

Getting the gcc Compiler

As an aside, when you install Apple’s Xcode (free from the Mac App Store only in Lion), gcc is automatically installed in /usr/bin. But if you have a mind to work with just Eclipse and gcc, you’ll need a way to install gcc without, if it pleases you, installing Apple’s Xcode first. (For reference, here’s a how-to on installing Xcode and gcc.)

There are at least two places I know of where you can get a gcc installer package for OS X:

If you find other sources, let us know.

Installing Eclipse

Here’s a handy reference on where to get Eclipse for the Mac. Version 3.7 (“Indigo”) installs nicely in Lion and seems to work okay, but our household hasn’t put it to a grueling acid test with OS X 10.7.1.

Eclipse -1

Eclipse Download for CC++, 64-bit

The download is a …tar.gz file, so move it where you want the Eclipse directory to be because when you double click it, the package will be unzipped and untared right there. After Eclipse is installed, you’ll see it as an app, just like any other, with this icon.

Eclipse-2

It’s easy to create an alias to eclipse.app, if you wish, and place it in your /Application directory. Because the Eclipse IDE itself is written in Java, if you’re running a clean copy of Lion, you’ll need to download the Java runtime before the Eclipse app will launch. Just double-click on Eclipse, and it’ll trigger the required Java runtime download. Nothing else to do.  If you’re in Snow Leopard, the Java runtime is already there.

Eclipse-3

Java runtime download

This first thing you’ll see when you run Eclipse is that it asks for the location of your workspace. I typically create a directory in /Users/john called Programming where I do any code development. (Which is not much these days.)

Eclipse-4

Workspace Selection

 

After you select your workspace, you’ll be confronted with an Eclipse peculiarity.  A welcome page will be launched that brings a real feeling of WTF to many users. Here it is.

Eclipse-8

Welcome screen

The trick is to realize that the icon on the far right, circled in red above, takes you into the workbench.  For that named workspace, this one time is the only time you’ll see that screen.

Once the IDE is launched, there are just a few details before you can build a project. Again, for simplicity, lets stay with C/C++. From the Eclipse File menu, select “New” and then C++ Project.

Eclipse-5a

Eclipse - New Project

Experienced Eclipse users know the drill. Name the project, make sure you’ve identified the location of your compiler in the Toolchain, and so on. (It may be different than the default install by Apple’s Xcode. The screen shot below shows Apple’s install of gcc, marked as MacOSX GCC, because I’d previously installed Xcode on that Mac.)

Eclipse-6

Configuration

Newbies may want to select the Hello World project just to see how things get set up. Make sure you work your way through the setup with Next > at the bottom and don’t click the Finish button too soon. A more detailed examination of this set up and the ins-and-outs of Eclipse are beyond the scope of this getting started article. The goal here is simply an introduction for Mac users. And you’re not alone. There’s a huge body of knowledge on the Internet about Eclipse. Here’s a start.

If all went well, you’ll be in the Eclipse IDE with the Hello World program in the editor and a Make file already created. This environment is called the Eclipse workbench.

Eclipse-7

Eclipse Workbench w/ default “Hello World” code

It may take you some start-up time to learn Eclipse, but trust me, coding the old way will soon bog you down. The Xcode and Eclipse developers learn from each other, so if you ever decide to get into OS X or iOS development, having earned your wings in Eclipse will pay huge dividends later.

__________________

* I’m qualified to say this because I’ve lived and breathed vi for a living in the past.

** Remember, a lot of people are going back to school, learning or refreshing their programming skills. 

*** You can write and compile Fortran 77 in Xcode with standard output, but you can’t access the Cocoa APIs and build native apps in F77.

 

Comments

webjprgm

I don’t like Eclipse, because it generally seems like it has a bloated UI and is slow. Sometimes it’s buggy if you get the wrong mixture of plug-ins.  But I know plenty of people who love it.

My strategy for simple code editing projects is either to use Xcode as an editor and not an IDE plus a Makefile (so it is cross-platform, and also so it’s simple) or to use TextWrangler (free version of BBEdit).  I just barely discovered Xcode 3’s “Organizer” window when they broke it in Xcode 4, but maybe the “Workspace” thing will work too.

It seems like there are three need to consider for the use-cases in this article:

1. Editor. 
  vi is harder to use, Eclipse and Xcode much easier. TextWrangler is just as easy.

2. Organizer.
  An IDE like Eclipse or Xcode make moving around your project much easier. With vi you have to fall back to the Terminal a lot. With TextWrangler the Finder is your organizer, though once you get into a project most of the files are open in the drawer.

3. Debugger.
  Eclipse and Xcode both have built-in GUI debuggers. Very helpful. With vi or TextWrangler you are left to gdb command-line interface and/or print statements. 

For #1 you don’t need Eclipse, for #2 you can mostly get by without Eclipse, for #3, well, debuggers are very helpful but I think it’s debatable whether they are worth the IDE for most simple projects.

The only debuggers that I’ve really liked are Firebug for Javascript in Firefox and Visual Studio for C#. Print statements are often fast enough and they are universal; not every language has a good debugger but they all have some kind of printf/logging mechanism.  It’s slower, but a simple method of copy-pasting-modify can pepper a chunk of code with “got here 1”, “got here 2”, ... and then run to see how far you get before things go wrong then you know where to look.  Then through in a bunch that say “x = %s”, “y = %d”, ... and see what value looks wrong.  It takes some recompiling iteration, but works quickly enough that I haven’t missed a debugger most of the time.  If I get a segfault I’ll run it in gdb since that will likely tell me the right place immediately.  Otherwise I use printfs.

In conclusion, I have nothing against debuggers, I just don’t like IDE. I don’t like project files, preferring the simplicity and near-universal cross-platform-compatibility of makefiles.  I don’t like a bloated interface, preferring just an editor.  If you find you like Eclipse, then go for it. Otherwise, try Xcode as just an editor and/or TextWrangler.

Dov

I appreciate the description of how to get Eclipse and gcc working on the Mac, since for some reason it has become more difficult to install unix packages lately.  But you seem to be confusing a GUI IDE with a debugger.  vi does not have a built in debugger.  emacs has a built-in everything—symbolic calculus, Mayan calendars, but also a debugger.  It may not be graphical, but it’s functional, and it’s always been faster and more powerful than Eclipse.

Having said that, the eclipse interface is easier to learn, the learning curve for emacs is steep, and eclipse is faster than it used to be.  I use both now.  Particularly for beginners, eclipse is useful because of command completion.

Saravanan Ganesan

The information has been very useful. Thank you.

Do you know how to set up C/C++ project with AutoTools in eclipse CDT

Saravanan Ganesan

I was trying to set up AutoTools in Eclipse CDT on Mac Maverick.

I followed the following link to download and set up autotools: http://jsdelfino.blogspot.com.au/2012/08/autoconf-and-automake-on-mac-os-x.html

I am able to set up autotools on terminal, but I am looking to use the autotools which I have set up on the terminal in Eclipse CDT.

How should I go about doing it or please share a simple hello world Autotools Project tutorial to begin with.

Thank you

alagalah

Hi, I have installed hpc gcc 4.9.0, and placed the bin directory at the top of my $PATH.

But when I run a test in eclipse to output the GCC version, it is using 4.2.1 (which I assume is the default system version for Xcode).

If I run gcc—version at a Terminal, I get 4.9.0.

How can I force Eclipse to use 4.9.0 as the default for ALL C++ projects (ie looking for Eclipse wide settings, not per Project settings).

Thanks

Log-in to comment