Article: C/C++ development with the Eclipse platform
Nov 13, 2003 — by LinuxDevices Staff — from the LinuxDevices Archive — 1 viewsThe C/C++ Development Toolkit (CDT) is an Eclipse plugin that extends Eclipse for use with C/C++, rather than Eclipse's native language, Java. This article outlines how to use Eclipse for C/C++ development, including how to use CDT.
How to use the 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 the Resources section later in this article for a link) is working towards 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 (Win 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 Java 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 the Windows platform, you'll need to install the Cygwin toolkit (see Resources for a link). 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 for a link). This is a collection of freely available and freely distributable Windows-specific header files and import libraries combined with GNU toolsets 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 from the Internet (see Resources for links).
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 metadata directories or.eclipse.cdt.*
from workspace/.metadata/.plugins
and features
.
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++.
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
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
author
ordate
keywords. 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
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, and then right-click and select Rebuild Project. All compilation messages coming from make, compiler, and linker are redirected to the console window:
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.
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 for links) 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 Platform community and download the Platform at eclipse.org. The Eclipse Platform source code is licensed under the Common Public License. At eclipse.org, you'll also find a glossary of terms and descriptions of Eclipse projects, along with technical articles and newsgroups. The Eclipse Platform white paper details the major components and functions of Eclipse.
- Download the components of the CDT Project from eclipse.org.
- Get access to a newsgroup for technical discussions and questions related to the C/C++ plug-in at eclipse.org.
- Developers can 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.
- The CDT Project uses bugzilla as its bug and feature tracking system. Entering a bug/feature report is as simple as filling in a Web form on the eclipse bugzilla page.
- For an introductory article on the Eclipse Platform and how it works, see the developerWorks article “Working the Eclipse Platform” by Greg Adams and Marc Erickson.
- To get started with developing applications using the Eclipse Platform, see the developerWorks article “Getting started with the Eclipse Platform” by David Gallardo.
- If you're interested in creating your own Eclipse plug-ins, see the developerWorks article “Developing Eclipse plug-ins” by David Gallardo.
- Download the Cygwin toolkit from Red Hat.
- Download the MinGW toolkit from MinGW.org.
- Learn more about the GNU make syntax at gnu.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.
- Learn about an extension to the IBM WebSphere Studio tools that provides information and facilities to help you work with them. Since all WebSphere Studio tools are based on the WebSphere Studio Workbench, and the Workbench is based on the Eclipse framework, it's easy to extend the product documentation by using the Eclipse help extension features. See the article “Adding Self-Help Support to WebSphere Studio Tools by Extending their Eclipse Help Systems“.
- Find more resources for Eclipse users on developerWorks.
About the author — Pawel Leszek, a Studio B author, is an independent software consultant and an author specializing in Linux/Win/Mac OS system architecture and administration. He has experience with many operating systems, programming languages, and network protocols, especially Lotus Domino and DB2. Pawel is also the author of series of articles for LinuxWorld and a Linux columnist for the Polish edition of PC World. Pawel lives in Warsaw with his wife and sweet little daughter. Questions and comments are welcome; email the author directly at [email protected]. |
First published by IBM developerWorks. Reproduced by LinuxDevices.com with permission.
This article was originally published on LinuxDevices.com and has been donated to the open source community by QuinStreet Inc. Please visit LinuxToday.com for up-to-date news and articles about Linux and open source.