C/C++ Users Journal November, 2004
The Eclipse platform is an integrated development environment for any type of project management. Although Eclipse generally targets Java development, through its plug-in extension architecture, Eclipse can support development for multiple languages and operating systems. One strong example is the C/C++ Development Tools (CDT) plug-in.
The CDT Version 2.0, part of the Eclipse Tools Project [1], supports the same OS platforms as Eclipse 3.0Windows, Linux, Solaris, QNX, AIX, HP-UX, and Mac OS X. It comes with a C/C++ editor, debugger (although a low-level debugging engine is required), C++ application launcher, automatic makefile generator, and various parsers and search engines to help in managing your C++ project.
What you don't get with the CDT is a C++ compiler, debugging engine, and make utility. The appropriate GNU versions of these components must be downloaded separately for your platform(s) [2]. For Windows C++ development, I downloaded the MinGW software [3], which comes with a set of Windows-specific header files and libraries, along with a complete set of GNU tools that let you build native executables and DLLs for Windows. I recommend downloading the set of GNU files marked as "current" [4]. The following files are the ones I downloaded and installed to compile my C++ projects (available at http://www.cuj.com/code/):
First, install the package labeled MinGW-3.1.0-1.exe into a directory such as c:\MinGw. Next, install the latest versions of the individual GNU components by extracting the contents of the compressed TAR files into the same directory just chosen. Next, run the three remaining installation programs, choosing the same directory as in the previous step. Finally, add the path to the MinGW bin directory (for example, c:\MinGW\bin) to your Windows system path. You now have a complete C++ development environment for Windows that Eclipse will use.
To install the CDT, shut down Eclipse and extract the contents of the CDT archive [5] into the plug-ins subdirectory of your Eclipse installation.
The Eclipse display paradigm defines different "perspectives" of project management. Eclipse has predefined perspectives for Java development, debugging, CVS repository exploring, and plug-in development. The CDT adds a new perspective for C/C++ development. Open this perspective by navigating to the Open Perspective option on the Windows menu within Eclipse. A submenu appears showing a subset of perspectives; click the last item on this menu, labeled "Other," to display the Select Perspective window with the full list of available perspectives. Select the perspective named "C/C++ Development." If you don't see it in the list, check your CDT installation.
The Navigator view contains a tab that displays all of your Eclipse projects. The CDT adds a tab to this display labeled C/C++ Projects, which shows only your C and C++ projects. This tab offers a richer display, listing the files within each project, along with the classes, methods, and variables declared and defined within each file. For C++ header files, class method and variable declarations are displayed using icons that indicate public, protected, or private access. For each C++ source file, method and variable declarations are listed, along with header files that are #included.
The display offers a well-balanced combination of file-based and class-based browsing within one tree view. A quick glance at the expanded tree details the layout of the project's files, the classes within those files, and members within the classes.
As with most features within Eclipse, the CDT settings can be modified through the Preferences dialog, available by selecting the Preferences option from the Window menu; see Figure 1. For instance, you can modify how your C/C++ code is displayed in the editor. Items that can be customized include text font, tab widths, code highlighting and color, and the display of warnings and errors.
The CDT also defines code templates for inserting frequently used code constructs such as for loops, if statements, and try...catch blocks. You can customize the code style for each of the available templates, as well as create new templates. Finally, you can define debugger settings, such as hex versus decimal display, as well as makefile and build settings, such as defining include paths and predefined symbols.
Eclipse defines two C/C++ project typesstandard and managed. A standard C/C++ project requires that you create and maintain the project makefile. This is useful if you want complete control over the build process, or if you have an existing project with a makefile already defined. For managed projects, the makefile is created and maintained by Eclipse. Various build settings can still be customized; however, this is done via the Project Settings user interface, as opposed to editing the makefile directlysomething most people have come to expect from an IDE.
Creating a new C/C++ project is similar to creating any new project in Eclipse. Select the New option from the File menu, then select Project from the submenu that appears. Within the New Project window, expand either the C or C++ option from the list and select a standard or managed project. At the next step, give the project a name and specify the project location. The default location for all projects created within Eclipse is in the workspace subdirectory of your Eclipse installation. However, you can optionally specify a project location other than the default. It's important to note that most of the GNU C++ tools for Windows cannot handle spaces in directory paths. Therefore, if your Eclipse workspace directory path contains any spacesc:\Program Files\Eclipse\workspace, for instanceyou must override the default at this point and provide a directory path without spaces.
The remaining steps in project creation differ for managed and standard C++ projects. For a managed project, the following steps remain:
For a standard project, the remaining steps are divided across tabs in the New Project window (see Figure 2):
Directory paths for include files and libraries can be set after project creation, through the Project Properties window. To do this, right-click on the appropriate project within the C/C++ Projects view, and select the Properties menu option.
To add a new class to your project, simply right-click on the project within the C/C++ Projects view, select the pop-up menu option, New, and the submenu option, Class. The New C++ Class window appears, letting you enter the class name, specify a base class, and optionally link the class to an existing source file, hence, importing the file. If you do not specify an existing file, a new one is created for your class. Once you click Finish, the class will be added to your project.
With a Managed C++ project, the makefile is created and maintained by the CDT automatically. With a standard project, however, this work is left to you. As an example, create a Standard C++ project. Add a new C++ class to the project named MyClass. The class file, MyClass.cpp, appears within the C/C++ Projects view and opens in an editor. To compile and link successfully, you must add the main function to the bottom of MyClass.cpp:
int main(int argc, int* args) {
return 0;
}
To aid in development, Eclipse combines source-based and file-based browsing in one display. Each file in a C++ project is listed within the Projects View. Expanding a file, however, visually displays all of the important C/C++ constructs within that file. Included files, constants, preprocessor definitions, global variables, and classes (including member variables and methods) are listed within the view, each with a unique icon. Clicking on one of the items causes the editor to open the file and position the cursor to the proper line. Table 1 contains a list of the most common C/C++ Projects View icons and a description of each.
Listing 1 contains the header file MyClass.h. Various C++ constructs have been added to the file to illustrate how the C/C++ Projects View visually aids in development. For instance, the following items have been added: Global variables (static and nonstatic); a #define, struct, typedef, and public, private, and protected class member variables and methods. Figure 3 is the C/C++ Projects View for MyClass, illustrating how each of these items is displayed in the view.
Next, create a makefile for your project to compile your project's code. To do this, right- click on your project within the C/C++ Projects View, select the menu option New, then the submenu option File. The New File dialog box appears; enter "makefile" within the File name edit box. Once the new makefile opens within an editor window, add the lines in Example 1, and save it. Be sure to use real tabs instead of spaces when indenting.
Finally, the project is ready to be built. Select the project name in the C/C++ Projects View, choose Project from the main Eclipse menu, and select the Build Project menu option. If you see the error message:
Exec error:Launching failed
it means that Eclipse cannot find the build command, which is make in this case. This indicates that there is a problem with your GNU installation. Review the steps in this article on downloading and configuring MinGW GNU compiler and tools.
In the case of compilation errors, Eclipse displays indicators in the individual editor windows next to the offending code and within the Tasks view. Figure 4 shows the error in the MyClass.cpp editor, along with the description in the Problems View, to follow. Clicking on an error in the Problems View opens the editor to the line of code where the compilation error occurred.
The Eclipse CDT supports source-level debugging, meaning it lets you set breakpoints and step through code line by line. Figure 5 shows a breakpoint added to the line of code in main that creates an instance of MyClass. To do this, right-click in the left margin of the code (just to the left of the text) and select the option Add Breakpoint. You can subsequently remove, or disable, a breakpoint the same way.
Once a breakpoint is set, you must start the program running in the debugger: Click on Eclipse's Run menu and select the Debug... menu option. When the Debug dialog box appears, right-click on the tree item labeled C/C++ Local, and select the New option from the pop-up menu. The project name appears in the Project edit box automatically, but you need to select the executable to run. Click the Browse button next to the C/C++ Application edit box and select the executable file, such as MyClass.exe, from the list of project files that appears (see Figure 6). This setup is only required the first time you debug a project; the settings are remembered from this point onward. Click the Apply button, and then click the Debug button to begin debugging the application.
Immediately, Eclipse changes to the Debug Perspective, with the code in an Editor window stopped at the breakpoint that was set. Eclipse indicates this by highlighting, and placing an arrow next to, the current line of code. The Debug Perspective also contains the Debug view, which displays running threads, and the Variables View, which displays variables, breakpoints, registers, memory sections, loaded libraries, and other useful debugging information. The values of variables can be inspected while stepping through the code, and can also be changed to test conditional code.
As you step through code within Eclipse, you can step into method calls by pressing the F5 key, or step over them via the F6 key. Resume application execution by pressing the F8 key, or terminate it by selecting Terminate from the Run menu.
The Eclipse C/C++ Development Toolkit, along with the GNU C/C++ tools, provides a stable, and very useful, C/C++ integrated development environment. Like other IDEs, the CDT offers tools such as code completion and content assist to remove the burden of typing. I find details such as the combined source and file-based code browsing to be better than in other IDEs.
Although I would like to see future versions of the CDT come with a compiler and tools, downloading and installing GNU separately is not difficult. It also provides a certain amount of flexibility and platform independence.
If you're a multiplatform C/C++ developer, try using Eclipse 3.0 with the C/C++ Development Toolkit. I'm sure you'll agree that it makes for a pleasant and powerful IDE, no matter which OS you develop on or target.