C and C++ in Eclipse

CDT stands for “C/C++ Development Tooling”. To download CDT, go to go do “Download Links” on the right side of this page. The eclipse….tar file in your Downloads folder should expand into an eclipse folder in your Downloads folder. In the eclipse folder, launch the Eclipse application. Its icon is a blue disk with horizontal lines.

Run a C++ program

Launch Eclipse.
Select a workspace:
/Users/myname/Documents/workspace on Mac
C:\Users\myname\workspace on PC
OK

Click the X on the tab of Eclipse’s Welcome pane to get it out of the way.

File → New → C++ project
Project name: Project
☑ Use default location
Project type: Executable → Hello World C++ Project
Finish

The left pane of Eclipse is the Project Explorer. In the Project Explorer, open Project → src → Project.cpp. In the top center pane, you should see the file Project.cpp containing the main function. Insert

#include <cstdlib>	//for EXIT_SUCCESS
immediately after the existing #include. Change
	return 0;
to
	return EXIT_SUCCESS;

File → Save
Project → Build All
Run → Run As → 1 Local C/C++ Application

Look for the standard output in the Console tab of the bottom center pane of Eclipse. If you don’s see this tab,
Window → Reset Perspective…
Yes
Window → Show View → Console

To run the program in the Macintosh Terminal application,
cd ~/Documents/workspace/Prog/Debug
pwd
ls -l Prog
./Prog
echo $? see the exit status

Run the C++ program in Chapter 1, pp. 85–88, Homework 1.7.3a

File → New → C++ Project
Project name: Term
Project type: Hello World C++ Project
Finish

In the Project Explorer pane of Eclipse, select the src folder of your project Term.
File → New → Header File
Header file: term.h
Finish
Overwrite the new file term.h with the contents of this file.

In the Project Explorer pane of Eclipse, select the src folder of your project Term.
File → New → Source File
Source file: term.c
Template: default C source template
Finish
Overwrite the new file term.c with the contents of this file. Uncomment the /* #define UNIX */.

Don’s advice

I took a couple of notes on how to make this work with Eclipse, just in case you get the same question in the future from other students. I couldn’t find any library file named libcurses.dylib so I started looking around on the Web. That led me to http://docstore.mik.ua/orelly/unix3/mac/ch05_02.htm which states: WARNING: In Mac OS X 10.1 and earlier versions, the curses screen library (a set of functions for controlling a terminal display) was part of libSystem.dylib. In Mac OS X 10.2 (Jaguar), the ncurses library (/usr/lib/libncurses.5.dylib) took the place of curses. You may still encounter source code releases that look for curses in libSystem.dylib, which will result in linking errors. You can work around this problem by adding -lcurses to the linker arguments. This is portable to earlier versions of Mac OS X as well, since /usr/lib/libcurses.dylib is a symbolic link to libncurses in 10.2, and to libSystem in earlier versions. The version of Eclipse that I’m using is Helios Service Release 2.

To add the -lcurses argument to the linker command, I did the following.

  1. Right-click (or control-click) on the active project in the Project Explorer view and then click on Properties.
  2. In the menu tree on the left side of the Properties window, navigate to CC/C++ Build Settings.
  3. In the Tool Settings tab, navigate to MacOS X C++ Linker Libraries.
  4. Press the Add button (it looks like a document with a green '+' in front of it) and enter curses. There’s no need to add the -l prefix, Eclipse will automatically add this when the linker is called.