Skip to main content

Build UNIX software with Eclipse

Bringing legacy code into a modern IDE

Chris Herborth (chrish@pobox.com), Freelance, Freelance Writer
Photo of Chris Herborth
Chris Herborth is an award-winning senior technical writer with more than 10 years of experience writing about operating systems and programming. When he's not playing with his son Alex or hanging out with his wife Lynette, Chris spends his spare time designing, writing, and researching (that is, playing) video games.

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® platform with a Java™ Runtime Environment (JRE) (Version 1.4 or newer) and an SWT port, such as Linux®, Solaris, AIX®, 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.

Date:  14 Mar 2006
Level:  Intermediate
Activity:  6501 views

Introduction

Historically, UNIX® 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™-centric build tool.

Before getting started

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® 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
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 and existing code

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:

  • Filesystem
  • zip archive
  • CVS repository

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.)

Creating a new project

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
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
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
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!
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
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
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
The project with files in it

Importing code from CVS

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
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 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
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
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
A new project filled with CVS data

Importing code summary

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.

Compiling with Eclipse

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 generic
make -f unix/Makefile test
make -f unix/Makefile install
make -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).


Figure 14. Creating build.xml with the New File wizard
Creating build.xml with the New File wizard

Right-click the new build.xml file and open it with the Ant editor (Open With > Ant Editor). Add targets for each of your makefile targets, as show in Listing 1.

Each <target> element will be available as an Ant build target. In this case, you're using the <exec> task, which executes an external program (make) using the arguments supplied in the <arg> elements. By default, they're executed in the directory that build.xml lives in, which is exactly what you want.


Listing 1. Ant build file for working with unzip


<?xml version="1.0"?>
<project name="unzip" default="make">
    <description>
        Ant adaptor for the unzip Makefile.
    </description>

    <target name="make" description="build unzip">
        <exec executable="make">
            <arg value="-f"/>
            <arg value="unix/Makefile"/>
	   <arg value="generic"/>
        </exec>
    </target>

    <target name="test" description="test unzip"
        depends="make">
        <exec executable="make">
            <arg value="-f"/>
            <arg value="unix/Makefile"/>
            <arg value="test"/>
        </exec>
    </target>

    <target name="install" description="install unzip"
        depends="make">
        <exec executable="make">
            <arg value="-f"/>
            <arg value="unix/Makefile"/>
            <arg value="install"/>
        </exec>
    </target>

    <target name="clean" description="clean unzip">
        <exec executable="make">
            <arg value="-f</arg>
            <arg value="unix/Makefile</arg>
            <arg value="clean"/>
        </exec>

    </target>
</project>

Now you can right-click the build.xml file and run it as an Ant build (Run As > Ant Build). The launch dialog (see Figure 15) lets you choose which build target to run.


Figure 15. The build targets for our unzip project
The build targets for our unzip project

You can see your build progress in the Console view, which will fill up with the output of your make command. As you can see from Figure 16, you've done it!


Figure 16. A job well done
A job well done

Building with a configure script

Another common way of building software from source is the GNU configure script. This extremely complex shell script is created through the autoconf utility and several customized input files. It digs through your system headers, compiles and runs several test programs, and generates a customized makefile for compiling this specific piece of software on your specific system.

The traditional use of a configure script is to run, build, and install the software:

sh configure --verbose
make
make install

The --verbose argument isn't really necessary; I just like to see what's going on, since configure scripts can take a while to complete. Check the installation instructions for your software to see if you need to supply any additional information to the configure script, such as compiler flags, the locations of libraries, and any software configuration options.

Like your previous makefile example, you're going to use an Ant build file to act as an adaptor between Eclipse and the configure script. Click the rx 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.

Right-click the new build.xml file and open it with the Ant editor (Open With > Ant Editor). Add targets for each of your makefile targets, as show in Listing 2.

Each <target> element in the build file shows up in Eclipse as an Ant build target. Again, you're using the <exec> task to execute an external program (the shell for your configure script, or make) using the arguments supplied in the <arg> elements.


Listing 2. Ant build file for working with rx

<?xml version="1.0"?>
<project name="rx" default="make">
    <description>
        ANT adaptor for GNU rx's configure/make process.
    </description>

    <target name="configure" description="configure rx">
        <exec executable="sh">
            <arg value="configure"/>
            <arg value="--verbose"/>
        </exec>
    </target>

    <target name="make" description="build rx"
        depends="configure">
        <exec executable="make"/>
    </target>

    <target name="install" description="install rx"
        depends="make">
        <exec executable="make">
            <arg value="install"/>
        </exec>
    </target>

    <target name="clean" description="clean rx">
        <exec executable="make">
            <arg value="clean"/>
        </exec>
    </target>

</project>

The targets in this build file are nearly the same as the ones seen earlier with the unzip example. You'll note that the make target lists configure as a dependency, so the configure target gets run first. Everything gets done for you (see Figure 17) when you run the make target -- you don't have to call it explicitly.


Figure 17. Running the make target
Running the make target

After you click Run, the make target's dependency and the configure target is run, filling the Console view with interesting messages about the things it's figuring out about your system, as they apply to this project. Once that's finished, the make target is triggered, which uses the makefile created by the configure target to build the software.


Figure 18. The result -- a library you can use
The result, a library you can use

Summary

Bringing a complex set of legacy code into a modern IDE like Eclipse doesn't necessarily require a huge amount of effort or retooling, if you can take advantage of an existing build process. Using Ant as an adaptor between the Eclipse IDE and your existing build process probably won't take you much time. It also gives you access to all of the existing capabilities. It might even let you simplify things a little, such as the configure script example you saw earlier, and provide some extra documentation for your build process.

UNIX developers looking to move to a friendlier, graphical IDE from the traditional command-line environment can use Eclipse with their existing code. You've seen how to create an empty project and looked at how to import your code from the filesystem, zip archive, or from source control in a CVS repository. Then you used Ant to build a project that uses a hand-crafted makefile and another that generates its makefile through a configure script.

By taking advantage of Eclipse's syntax highlighting, code completion, and other amenities provided by a top-notch IDE, developers can quickly become more productive with their own code and others.



Downloads

DescriptionNameSizeDownload method
Ant build file for Info-ZIPes-unzip-build.zip1KB HTTP
Ant build file for the GNU rx libraryes-rx-build.zip1KB HTTP

Information about download methods


Resources

Learn

Get products and technologies

  • Build your next development project with IBM trial software, available for download directly from developerWorks.

Discuss

About the author

Photo of Chris Herborth

Chris Herborth is an award-winning senior technical writer with more than 10 years of experience writing about operating systems and programming. When he's not playing with his son Alex or hanging out with his wife Lynette, Chris spends his spare time designing, writing, and researching (that is, playing) video games.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=AIX and UNIX
ArticleID=105706
ArticleTitle=Build UNIX software with Eclipse
publish-date=03142006
author1-email=chrish@pobox.com
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Special offers