Bringing legacy code into a modern IDE
Summary: Become more productive with your own code and others by utilizing Eclipse's syntax highlighting, code completion, and other amenities. Eclipse is an excellent open source IDE and has many helpful features. It runs on any UNIX?0?3 platform with a Java?6?4 Runtime Environment (JRE) (Version 1.4 or newer) and an SWT port, such as Linux?0?3, Solaris, AIX?0?3, and HP-UX. It's easy enough to start a new project using Eclipse or to import an existing Eclipse project, but how do you bring existing code into the IDE? And what if you need to get an existing project compiling right away without modifying its existing makefile or configure script -- the most common ways of building UNIX software? Read along for the answer to all of these questions.
Historically, UNIX?0?3 development has been done with your favorite text editor and command-line tools, such as make
. Even after UNIX entered the GUI era with X11 and various window managers, development stayed back at the command line. This was fine for seasoned developers working with complex editors like EMACS and vi; however, it added an extra productivity roadblock for folks who did not already know how to use these tools.
Using Eclipse gives you the advantages of a graphical interface, editors that support syntax highlighting, code completion (and optional EMACS-style key bindings), and a common platform for development with any programming or scripting language.
Migrating your development environment from a traditional UNIX editor, command line, or makefile system to an integrated GUI like Eclipse can be daunting. You're not sure if you can be as productive, you don't know if you can bring your existing code into the IDE, and maybe you're worried about needing to modify your project to get it compiling from the GUI.
Luckily, the flexibility of Eclipse gives you quite a few options for bringing existing code into the IDE. You're going to look at three different ways for bringing UNIX code into Eclipse in this article: from the filesystem, a zip archive, and a CVS repository. You'll also look at using the Ant build system to adapt an existing makefile or configure script to life in Eclipse, despite its reputation as a Java?6?4-centric build tool.
If you want to follow along with this article, you'll need to pick up a few things. Everything used here is either freely available open source, such as the Eclipse IDE, or something you can supply yourself, such as the existing programs you'll be bringing into the IDE and compiling.
If it's not already installed on your system (some Linux?0?3 distributions already include it), visit the Eclipse site (see Resources) and download Eclipse with the Java development environment. Even though this article focuses on the typical C project, you're going to use part of the Java Development Kit (JDK) to help build your project. The "Eclipse SDK" package or the "Platform Runtime Binary" and "JDT Runtime Binary" packages (if you'd like to save a little space at the expense of having to manage two downloads and installations) is what you're looking for.
Next, you're going to need the Eclipse C/C++ Development Tools (CDT). If you visit the URL listed in Resources, you can either download the packages or use Eclipse's update mechanism to install the CDT (see Figure 1).
If you've never used Eclipse before, take some time to play with the user interface and try out a couple of the tutorials in the online help. That will get you familiar with the way it works and the sorts of things it can do.
Figure 1. The Eclipse CDT in action
Finally, you should have a project or two that you can bring into Eclipse. This article uses GNU's rx
library (a fast replacement for the GNU regular expression library) to demonstrate importing from the filesystem (and building software that uses a configure script). It uses Unzip from the Info-ZIP group for importing software from a zip archive, and building software that uses a makefile. And it uses ScummVM to demonstrate importing code from a CVS repository such as SourceForge.
Eclipse can cause some confusion for folks migrating from the command line or another IDE. Unlike your favorite text editor and the make
command, Eclipse likes to keep all of your projects together in a workspace directory. By default, the workspace directory is located in your home directory, and inside of it will be one directory per project that Eclipse knows about.
Technically, if you've created an empty project in Eclipse, you can drop additional files into the project's directory, refresh the project in Eclipse, and be ready to go. It's easier to import the files using Eclipse.
In the following sections, you'll see how to create a new, empty project and three different ways of bringing existing source code into that empty project and thus, into the realm of Eclipse. Listed below are three different techniques you'll import from:
The first two options (filesystem and zip archive) are similar, so use whichever one is more convenient for you. Importing from a CVS repository is a requirement for people working in a team environment using CVS as the source control system. You'll be able to synchronize your changes with those of your team directly in Eclipse. (There are source control plug-ins for other systems, such as Subversion and Perforce; see Resources for a link to Eclipse Plug-in Central.)
Let's say you've got some existing code. You want to see if you can build it inside of Eclipse in order to evaluate the IDE, or so you can continue working on the project in a more comfortable development environment.
Before you can import the files, you're going to need to create a new project so Eclipse has somewhere to import the code to. Use the File > New > Project menu item to display the New Project wizard (shown in Figure 2), select a generic project (Simple > Project), and assign it a name. So it's stored in your normal workspace directory, leave it in the default directory.
Figure 2. The New Project wizard supports many different kinds of projects
Importing code from the filesystem
You most likely have some code laying around in a directory that you can import into Eclipse using the Import wizard (File > Import), as shown in Figure 3. Select the "File system" source, then click Next to select a directory to import.
Figure 3. Importing from a directory in the filesystem
After you've selected a directory, Eclipse gives you a list of the subdirectories (see Figure 4). You can choose a subdirectory to get a list of files in that directory. Files that are checked will be imported into your Eclipse project, so you can use this dialog to zoom in on the resources you're interested in. Click Finish when you're done selecting files and directories (I've simply selected the root directory and left everything else checked), and Eclipse copies them into your project.
Figure 4. Selecting which files and directories to import
When the files and directories have been imported, your project displays them all (see Figure 5). At this point, you're ready to get things going; feel free to skip down to the Compiling with Eclipse section.
Figure 5. The project -- now with files!
Importing code from an archive
Importing your source code from a zip archive is as straightforward as importing it from the filesystem. In the Import wizard (File > Import), choose the "Archive file" source, as shown in Figure 6, then select the zip file containing the code you want to import.
Figure 6. Importing from a zip archive
After you've selected a zip archive, Eclipse displays a list of the files and directories inside (see Figure 7). Use this dialog to specify which directories and files you want to import by selecting them, then click Finish when you're done. Eclipse extracts the files from the archive and imports them into your project.
Figure 7. Selecting the files and directories to import
When the files and directories have been imported, your project displays them all (see Figure 8). At this point, you're ready to get things going; feel free to skip down to the Compiling with Eclipse section.
Figure 8. The project with files in it
Lots of development projects use CVS to provide version control of services. Eclipse has built-in support for importing code from CVS servers. Use the Import wizard (File > Import) and choose "Checkout Projects from CVS" as the import source (see Figure 9). Click Next to continue.
Figure 9. Importing from a CVS repository
You can either choose a previously configured CVS server as the source repository, or you can create a new repository location (see Figure 10). In this example, you're downloading a project from SourceForge.net, so select "Create a new repository location" and click Next to fill in the details.
Figure 10. You can choose an existing CVS repository or access a new one
You'll need to provide the CVS server's host name, the path to the CVS repository, and authentication information (see Figure 11), all of which can be discovered from the project's Web pages or your local CVS administrator.
Figure 11. Entering the CVS server information
Clicking Next lets you select the CVS module (or project) you want to import (see Figure 12). If you're not sure of the name, choose "Use an existing module" to have Eclipse query the server for a list of available modules. Click Finish to start downloading the module's files.
Figure 12. Selecting a CVS module
If you've already got a project with the same name as the module you're checking out, Eclipse warns you about overwriting it when you click Finish. If you had an existing project with the same name, be careful about choosing OK, because you won't be given a chance to reconsider.
When the files and directories have been imported, your project displays them all (see Figure 13).
Figure 13. A new project filled with CVS data
Before you can import code into Eclipse, you need to create an empty project. This will be the container for the code you import.
If the code exists somewhere in a filesystem that you can read (either attached to your workstation or a network filesystem), import it directly into your Eclipse workspace from the filesystem.
If the code exists in a zip archive either downloaded from the Internet or sent to you from another developer, Eclipse can extract the files and import them from the archive without needing you to unpack it first.
If the code exists in a CVS repository, Eclipse can connect to the CVS server and import your code, with full source control capabilities.
Now that you've got some code imported into an Eclipse project, you'll want to be able to do useful things with it. Double-clicking a file to open it with an appropriate editor is intuitive enough, but how are you going to get it building?
If you're starting a new project in Eclipse, you can take advantage of its "Managed Make" facilities to handle building your application automatically, or you can use the normal C/C++ Make projects ("Standard Make" in Eclipse) to build your application using the make
utility.
Unfortunately, if you're importing an existing project, things are a little more complicated. Existing projects will have existing build processes, which might be very complex and not well-suited for an IDE. On the other hand, an existing project is likely to compile without many problems, so you just need a way to hook into the existing build process from within Eclipse.
The Ant utility, provided as part of the Java development environment, can help out here. Although it has a reputation as being a build utility for Java, it's actually quite flexible, much like the traditional make
command. You're going to look at using it to launch the standard make
command (and handle all of the usual make
targets, such as install and uninstall) and to run configure script-based builds.
One last thing about Eclipse. As you write and compile your program, Eclipse builds an outline (in the Outline view) for each source file. The outline shows all of the headers you've included and all of the symbols defined in that source file. Once this outline shows your headers, Eclipse has indexed those headers and code completion now works for symbols defined in the header files. For example, typing print
and then pressing Ctrl+Space causes Eclipse to complete printf()
and show printf()
's prototype in a pop-up window. This is extremely handy if you're like me and have an easier time remembering which function you need to use instead of the function's prototype.
Building with an existing makefile
The unzip project you imported earlier is a free extractor for the zip archive format invented by PK-WARE back at the dawn of time. It's handy to have installed on your UNIX system, because zip files are very common for distributing files on the Internet.
The code uses a makefile (found in the UNIX directory) to build and install appropriate binaries for your system. It knows about dozens of different, and sometimes esoteric (such as the "wombat"), UNIX variants, but it's already presenting a problem. According to the installation directory, you're supposed to invoke it from the main source directory, not the UNIX subdirectory:
make -f unix/Makefile genericmake -f unix/Makefile testmake -f unix/Makefile installmake -f unix/Makefile clean |
If you're going to build unzip from Eclipse, you need to make an Ant build file that supports these helpful targets.
Click the unzip project in the C/C++ Projects or Navigator view, then create a new file (File > New > File) named build.xml, the default build file for Ant (see Figure 14).