Editor's note: An updated version of this article titled "Developing applications using the Eclipse C/C++ Development Toolkit" was published in July 2007. This original version will remain available for reference purposes. Read the new article for the latest information about using the C++ Standard Template Library (STL) and the Eclipse C/C++ Development Toolkit (CDT).
The C and C++ languages are among the most popular and widely used programming languages in the world, so it's not surprising that the Eclipse Platform provides support for C/C++ development. Because the Eclipse Platform is only a framework for developer tools, it doesn't support C/C++ directly; it uses external plug-ins for support. This article shows you how to make use of the CDT -- a set of plug-ins for C/C++ development. The CDT Project (see Resources) is working toward providing a fully functional C/C++ Integrated Development Environment (IDE) for the Eclipse Platform. Although the project focus is on Linux®, it works in all environments where GNU developer tools are available, including Win32 (Windows® 95/98/Me/NT/2000/XP), QNX Neutrino, and Solaris platforms.
The CDT is an open source project (licensed under the Common Public License) implemented purely in the Java programming language as a set of plug-ins for the Eclipse SDK Platform. These plug-ins add a C/C++ Perspective to the Eclipse Workbench that can now support C/C++ development with a number of views and wizards, along with advanced editing and debugging support.
Due to its complexity, the CDT is broken down into several components, which take the form of separate plug-ins. Each component operates as an autonomous project, with its own set of committers, bug categories, and mailing lists. However, all plug-ins are required for the CDT to work properly. Here is a complete list of the CDT plug-ins/components:
- Primary CDT plug-in is the "framework" CDT plug-in.
- CDT Feature Eclipse is the CDT Feature Component.
- CDT Core provides Core Model, CDOM, and Core Components.
- CDT UI is the Core UI, views, editors, and wizards.
- CDT Launch provides the launch mechanism for external tools such as the compiler and debugger.
- CDT Debug Core provides debugging functions.
- CDT Debug UI provides the user interface for the CDT debugging editors, views, and wizards.
- CDT Debug MI is the application connector for MI-compatible debuggers.
Now, let's see how you can put these components to use in real applications. Figure 1 shows a C/C++ project in Eclipse:
Figure 1. Editing a C/C++ project in Eclipse with the CDT plug-in
Installing and running the CDT
Before you download and install the CDT, you first need to ensure that you
have the GNU C compiler (GCC) and all the accompanying tools (make,
binutils, GDB) available. If you're running Linux, simply install the
development packages using the package manager appropriate for your
distribution. On Windows, you'll need to install the Cygwin toolkit (see
Resources). Cygwin is a UNIX®-like
environment for Windows that includes a GCC port, along with all necessary
development tools, including automake and the GNU Debugger (GDB). Cygwin
is built around the
cygwin1.dll library. The alternate
solution for Cygwin is Minimalist GNU for Windows (MinGW) (see Resources). This is a collection of freely
available and freely distributable Windows-specific header files and
import libraries combined with GNU tool sets that allow you to produce
native Windows programs that do not rely on any third-party DLLs. MinGW is
your best choice if you want to create Windows applications that are
POSIX-compatible. MinGW can even work on top of a Cygwin installation.
Solaris and QNX require you to download and install their specific GCC,
GNU Make binutils, and GDB ports (see Resources).
It's assumed that you have the appropriate Java SDK/JRE and Eclipse
Platform SDK installed and that they are running without problems. The CDT
is available in two "flavors," as stable releases and nightly builds.
Nightly builds have not been fully tested, but they offer more features
and correct current bugs. Before installation, check if the previous
version of the CDT exists on your disk and, if so, make sure to completely
remove it. Because there are no uninstallers available for the CDT, you
will need to do this manually. To check to see if a previous version
exists, go to the directory where the CDT plug-ins reside:
eclipse/plugins. Next, remove all directories starting with the
org.eclipse.cdt name. The last thing you will need to do is remove the CDT
The next step is to download the CDT binaries. Take care to download the
correct CDT for your operating system. Unfortunately, CDT is not platform
independent, even though it is written in Java. Next, uncompress the
archives into a temporary directory and move all plug-in directory content
from there to the Eclipse
plugins subdirectory. You will also
need to move the
features directory content to the Eclipse
features subdirectory. Now restart Eclipse. After Eclipse
starts again, the update manager will tell you that it has found changes
and will ask you to confirm them. You'll now be able to see that two new
projects are available: C and C++.
Creating new projects
After installing the CDT in Eclipse, navigate to File > New > Project, where you will find three new types of projects available: C ("Standard C Make Project"), C++ ("Standard C++ Make Project"), and "Convert to C or C++ Projects". Start with "Standard Make C++ Project" to create source code files for your project. In the C/C++ Projects View, right-click and select New > Simple > File. Name your file and save it. You'll probably create many files for header and C/C++ implementation code files in this way. The last one is, of course, makefile, which will be used by GNU Make for building binaries. Use the usual GNU make syntax for this makefile (see Resources). Remember that makefiles require you to make indented lines using the Tab character and not spaces.
You will more often import existing source code into Eclipse (see Figure 2). The CDT offers an easy way to perform this operation using the Import wizard to copy files from a file system directory into the Workbench. Go to the main menu bar and select File > Import > File System. Click Next and open the source directory and select the directories from which you would like to add files. Click Select All to select all resources in the directory, and then go through and deselect the ones that you do not want to add. Specify the Workbench project or folder that will be the import destination. You can also import folders and files by dragging them from the file system and dropping them in the Navigator view, or by copying and pasting.
Figure 2. Importing existing source code into a CDT project
Key CDT IDE features
The CDT IDE is build around a universal extensible editor provided by the CDT UI plug-in. This module is still under development, however, so it is still missing some important utilities, such as a class browser or a language documentation browser. The main features of the CDT IDE are:
- Syntax highlighting -- CDT IDE recognizes C/C++ syntax and provides syntax highlighting with fully configurable code colorizing and code formatting:
Figure 3. Syntax error mark highlighted after unsuccessful compilation
- Outline -- The Outline window module provides a quick view on procedures, variables, declarations, and functions that appear in source code. With outline you can easily jump into the appropriate reference in the source code or even search all project source code.
- Code assist -- This code completion feature is similar to ones found in Borland C++ Builder or MS Visual Studio. It uses code templates and simply helps to avoid silly syntax errors:
Figure 4. Code assist feature helps to use proper language syntax
- Code templates -- The code templates, which are used
by the code assist feature, are definitions of standard C/C++ language
syntactical constructions. You can also define your own code templates
for expanding your own shortcuts, such as ones for
datekeywords. In Window > Preferences > C/C++ > Code Templates, you can add new templates and view the full list of templates. Templates can also be exported and imported as XML files.
Figure 5. Pre-defined C/C++ code templates
- Code history -- Even if you do not use CVS or another source code version management software, you can trace local changes in a project's source code. Right-click on the selected file and choose Compare With > Local History... from the context menu:
Figure 6. Checking changes in source code with Local History function
Building and running projects
The CDT offers an easy way for setting project building options. The CDT
relies on three GNU tools: GCC, GDB, and Make. Therefore, the dependencies
on GDB for debugging, or GCC and Make for compilation, require that these
applications are available for the platform that the user wishes to use.
Most Linux (and, in general, POSIX-like) source code packages use the
autoconf script for checking the build environment, so you need to run the
configure command, which creates the "Makefile" before
compilation. CDT doesn't offer a way for editing autoconf scripts, so you
need to write them manually. However, you can configure building options
to call the
configure command before compilation.
If you build a project by invoking the
make command, the
default settings are okay, but if you use a more complicated way, you need
to enter the appropriate command (for example,
make -f make_it_all) in the Build Command text box. Next, in
the C/C++ Projects View, select C/C++ project, then
right-click and select Rebuild Project. All compilation
messages coming from make, compiler, and linker are redirected to the
Figure 7. Console window with output from the compiler
After successful compilation, you will probably want to run your application. All options for running and debugging are located under the Run menu in the main Eclipse menu. However, options for running a project must be defined earlier. You can do this by going to the Run... option in the main menu where there are different profiles for running applications; for example, you can use one profile for testing purposes and the other for running the final version. Additionally, you can define the arguments you want to pass to an application, or you can set the environment variables. The other options are for setting debugging options, such as which debugger to use (GNU GDB or Cygwin GDB). Figure 8 shows a run profile being created for a project.
Figure 8. Creating a run profile for your project
More general options for building projects are available when you go into the C/C++ Projects View, select your project, and right-click and select Properties on the Build Settings tab. These options mostly affect what happens when a build stops when a compilation error is encountered.
Debugging C/C++ projects
The CDT extends the standard Eclipse Debug View with functions for debugging C/C++ code. The Debug View allows you to manage the debugging or running of a program in the Workbench. To start debugging the current project, simply switch to the Debug View and you will be able to set (and change at anytime during execution) breakpoints/watchpoints in the code and trace variables and registers. The Debug View displays the stack frame for the suspended threads for each target you are debugging. Each thread in your program appears as a node in the tree, and the Debug View displays the process for each target you are running.
Eclipse supports Machine Interface (MI)-compatible debuggers through one of its components: the CDT Debug MI plug-in. But what exactly is an MI debugger? Traditionally, third-party GUI debuggers like ddd and xxgdb (see Resources) have relied on GDB's Command Line Interface (CLI) when implementing debugging functionality. Unfortunately, that interface has proven to be highly unreliable. GDB/MI provides a new machine-oriented interface that is far better suited to programs that want to directly parse GDB's output.
The C/C++ Development Toolkit (CDT) provides a powerful set of plug-ins that can help you develop C/C++ applications with Eclipse. Although the CDT is still under development, you can take advantage of many of its features today.
- Join the Eclipse community and download it at Eclipse.org. The Eclipse source code is licensed under the Common Public License. You'll also find a glossary of terms and descriptions of Eclipse projects, along with technical articles and newsgroups.
- The CDT Project uses Eclipse Bugs as its bug- and feature-tracking system. Entering a bug/feature report is as simple as filling in a Web form.
- To get started with developing applications using Eclipse, read "Getting started with the Eclipse Platform" by David Gallardo.
- If you're interested in creating your own Eclipse plug-ins, read "Developing Eclipse plug-ins."
- Learn more about the GNU make syntax at gnu.org.
- Expand your Eclipse skills by visiting IBM developerWorks' Eclipse project resources.
- Browse all of the Eclipse content on developerWorks.
- Visit the developerWorks Open source zone for extensive how-to information, tools, and project updates to help you develop with open source technologies and use them with IBM's products.
- Stay current with developerWorks technical events and webcasts.
Get products and technologies
- Download the components of the CDT Project from Eclipse.org.
- As someone interested in C/C++ development, you might want to check out a trial of IBM's XL C/C++ compiler for Linux or AIX.
- Download the Cygwin toolkit from Red Hat.
- Download the MinGW toolkit from MinGW.org.
- Download the ddd GUI debugger from gnu.org.
- Download the xxgdb GUI debugger.
- Visit WebSphere Studio Plug-in Central for a directory of plug-ins that have been validated as Ready for WebSphere Studio Software.
- Innovate your next open source development project with IBM trial software, available for download or on DVD.
- The Eclipse Community forums have many resources for people interested in using and extending Eclipse.
- Get access to a newsgroup for technical discussions and questions related to the C/C++ plug-in at Eclipse.org.
- Participate in discussions relating to the implementation of the C/C++ IDE via the developer mailing lists created for each of the components in the project.
- Get involved in the developerWorks community by participating in developerWorks blogs.