en|de

C++ Programming with Eclipse CDT

Martin Kompf

The Eclipse C/C++ Development Toolkit (CDT) combines the look and feel of Eclipse with the power of the GNU compiler.

The Eclipse IDE for Java developers is a well-known, freely available toolkit for software development. Main features are the powerful editor with code completion, project management and the large number of plugins. But Eclipse is also interesting for C and C++ developers: The C/C++ Development Toolkit (CDT) allows the user to develop and manage programs written in C or C++. The Managed Make Projects are particularly noteworthy, because they relieve the program author from the unloved and error-prone creation of Makefiles.

Eclipse CDT requires as a prerequisite an installed C (gcc) and C++ compiler (g++) and various other utilities such as make, ld, cpp. Of course a Java Runtime Environment (JRE) is necessary for the operation of the Eclipse IDE.

The procedure for installing these components depends on the operating system and the existing software.

Linux has everything on board

On current Linux distributions all components are already available as pre-built packages. On Debian or Ubuntu, for example, you need to install the packages eclipse-cdt, g++ and make. Then all necessary tools are on board.

Windows: Java, Eclipse and MinGW

On Windows, you should - if not already done - first download and install a recent Java Runtime Environment from Oracle Java SE Downloads. Then download preferably the pre-packed Eclipse IDE for C/C++ Developers from the Eclipse download page and unzip it, for example, into C:\Program Files\eclipse.

If Eclipse is already installed, you can also add the CDT later. The way forward is through the menu Help - Install New Software. In the opening dialog first select the download site matching the installed Eclipse version. After selecting the entry C/C++ Development Tools (in section Programming Languages), the rest of the installation is straight forward.

Now it still lacks the C++ compiler. If you want to use a free version here as well, then the MinGW - Minimalist GNU for Windows - is recommended. This is a collection of freely available compilers, tools, headers and libraries based on GNU for creating Windows programs. The installation requires only a few steps:

Download the MinGW setup program mingw-get-setup.exe and execute it. It installs the MinGW Installer first. This installer is started directly from the setup with Continue or by double clicking the desktop icon. In the GUI select first the item Basic Setup and then the components mingw32-base, mingw32-gcc-g++ and msys-base. After activating the menu Installation - Apply Changes the program downloads the selected components from the MinGW site and installs them.

If you have accepted the default folder C:\MinGW as the installation location, then you have to append the directory C:\MinGW\bin to the PATH environment variable. (You may edit the PATH variable with Windows key - Environment variables.)

Now go!

Now everything should work: After starting Eclipse you should activate the C/C++ perspective. Now we create our first C++ project: File - New - C++ Project. The project name is arbitrary, FirstCDTProject for example. As toolchain we choose MinGW GCC. If this entry is missing, then Eclipse didn't find the MinGW installation. In this case you should check the PATH environment variable again and restart Eclipse. The new project appears now with a set of standard include directories in the project explorer. You may create a new C++ source code file by clicking on the project folder with the right mouse button and selecting New - Sourcefile. We name it first.cpp and enter the following content:

#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char **argv)
{
    char *c;
    string greeting("Hallo ");
    
    if (NULL != (c = getenv("USER"))) {
        greeting.append(c);
    } 
    else if (NULL != (c = getenv("USERNAME"))) {
        greeting.append(c);
    }   
    else {
        greeting.append("wer bist du denn?");
    }
            
    cout << greeting << endl;
    
    return 0;
}

After saving the program is compiled and linked via Project - Build All or the shortcut Ctrl B. Any errors are marked with a red marker. This also works for larger projects, which consist of several source files! By Right mouse button -Run As the program may be run.

If you have transported the source code via copy&paste into the CDT, you missed an interesting feature: The Code Completion that makes appropriate suggestions for completing the program code as you type, when activating the key combination Ctrl Space:
Eclipse Code Completion

With the CDT a powerful IDE is available for C and C++ programmers that appears immediately intuitive to the experienced Eclipse user. It works on both Linux and Windows alike - unfortunately it relieves the programmer not from porting its programs between the two operating systems.