Getting Started with C++ for Eclipse

by Deepak Vohra
Getting Started with C++ for Eclipse

Begin your course of study about using C++ for Eclipse.

C++ is a C-based programming language that provides a direct mapping of hardware features and a low-level memory manipulation, and is also a general purpose programming language that could be used in various contexts. C++ inherits from C and also introduces object-oriented programming features with the use of classes that provide the common OOP features of abstraction, encapsulation, inheritance, and polymorphism. C++ is ranked as the third most commonly used programming language by TIOBE Index of programming languages. C++ could be used in various IDEs, such as Visual Studio, Eclipse, and NetBeans. In this tutorial, we shall get started with using C++ in Eclipse IDE. This tutorial has the following sections.

Installing Eclipse IDE

Eclipse provides the Eclipse IDE for C/C++ Developers for C/C++ development. Download and install the Eclipse IDE for C/C++ Developers. We have used the 64-bit Windows OS. For Windows 64-bit, download and extract the eclipse-cpp-neon-1a-win32-x86_64.zip file to a directory; for example, the C:\C++\eclipse directory.Double-click the C:\C++\eclipse\eclipse application to start the Eclipse IDE.

Installing MinGW

We need to install MinGW, which is a short term for Minimalist GNU for Windows, a development environment for native Windows applications with support for the GNU Compiler Collection (gcc) including C, C++, and Cross G++ compilers. Download the mingw-get-setup.exe application.

Double-click the mingw-get-setup.exe application to launch the MinGW installer. In the MingGW Installation Manager Setup Tool, click Install.

Figure 1: Launching the MinGW installer

Select an Installation Directory (C:\MingGW) and click Continue. We shall use the installation directory when creating a C++ project in Eclipse to set the PATH environment variable.

Figure 2: Setting the PATH environment

The MinGW Installation Manager begins to get downloaded and set up. Click Continue when the download is complete.

Figure 3: Completing the download

The MingGW Installation Manager lists the Packages in two categories, Basic Setup and All Packages.

Figure 4: Listing the Packages

Select the Basic Setup, right-click, and select Mark for Installation to select a package to be installed.

Figure 5: Marking for installation

Select all the Basic Setup Packages. Select Installation>Apply Changes to apply the packages.

Figure 6: Selecting Installation>Apply Changes

Click Apply.

Figure 7: Applying the changes

The packages get downloaded and the scheduled changes get applied.

Figure 8: The packages are downloading

After all the packages get applied, select Installation>Quit.

Figure 9: The packages have been applied

Creating a C++ Project in Eclipse

In this section, we shall create a C++ project in Eclipse for a Hello World application. Select File>New>C++ Project.

Figure 10: Selecting File, New, C++ Project

In C++ Project, specify a Project name (HelloWorld, for example). The project name should not have any spaces because the C++ file (.cpp) also is created by the same name. Use the default location to create the project. For Project type, select Executable>Hello World C++ Project, and in Toolchains, select Cross GCC. Click Next.

Figure 11: Selecting Executable, Hello World C++ Project

In Basic Settings, specify some basic settings. The Hello world greeting is the message output by the C++ application and the Source is the folder in which the C++ source code file is created. Select the default settings and click Next.

Figure 12: Specifying basic settings

In Select Configurations, the Project type is Executable, and the Toolchains, it is Cross GCC. The Debug and Release configurations are selected by default. Click Next.

Figure 13: Selecting configurations

In Cross GCC Command, specify the Cross compiler prefix as mingw32- and the Cross compiler path as C:\MingGW\bin. The prefix is obtained from the mingw32-g++ .exe file and the mingw32-gcc .exe file for in the C:\MingGW\bin directory. Click on Finish.

Figure 14: Specifying the Cross compiler settings

A new C++ project—including a source file, HelloWorld.cpp—gets created.

Figure 15: The HelloWorld.cpp file is created

The objective of the tutorial is not to discuss the C++ syntax or language features but to introduce using C++ in Eclipse. The HelloWorld.cpp script is listed.

#include <iostream>
using namespace std;
int main()
   cout << "!!!Hello world!!!" << endl;
   return 0;

The project environment must include the PATH variable with the C:\MingGW\bin directory in the value. Right-click the project in Project Explorer, and select Properties. In Properties, select C/C++ Build>Environment. The PATH environment variable should include the C:\MinGW\bin.

Figure 16: Checking the PATH variable

Select C/C++ General>Preprocessor Include Paths, Macros, and so forth. Also, the CDT Cross GCC Built-in Compiler Settings should be selected.

Figure 17: Selecting the settings

Select Project>Build Project or Project>Build All to build (compile) the project. The HelloWorld.exe binaries get generated in the Debug directory.

Figure 18: The HelloWorld.exe binaries are generated

Running the Application

To run the C++ application, we shall need to run the HelloWorld.exe binaries and not the source code file HelloWorld.cpp. Select Run>Run Configurations to create a run configuration for the application.

Figure 19: Running the binary version of the file

In Run Configurations, right-click C/C++ Application, and select New.

Figure 20: Selecting New

Specify a Run Configuration Name (HelloWorld) and, in the Main tab, specify or select the Project as HelloWorld. If the application requires any args, those could be set in the Arguments tab and any environment variables could be set in the Environment tab. In C/C++ Application, specify Debug/HelloWorld.exe and click Apply.

Figure 21: Clicking Apply

Click Run to run the configuration.

Figure 22: Clicking Run

The Save and Launch window gets opened; in it, the source code file HelloWorld.cpp is listed. Select the source code file and click OK.

Figure 23: Selecting the source code

The HelloWorld.cpp file gets built with the Cross G++ Compiler and the compiled application gets run.

Figure 24: The compiled application is run

The "!!!Hello World!!!" message gets output in the Console.

Figure 25: The "!!!Hello World!!!" message is output

Updating the Application

If an application needs to be updated frequently and re-run, select Project>Build Automatically so that the application does not have to be built with each modification.

Figure 26: Selecting Build Automatically

Next, we shall modify the C++ application source code HelloWorld.cpp slightly and re-run the application. Modify the application as follows.

#include <iostream>
using namespace std;

int main()
   cout << "Hello world" << endl;
   cout << "From a C++ Program" << endl;
   return 0;

The updated application is about the same, except with an additional line of output.

Figure 27: The new application has an additional line of code

Save the updated application with File>Save All.

Figure 28: Saving all the files

Click Run in the same Run Configuration window to run the application.

Figure 29: Selecting Run

The application gets rebuilt and a different output is generated in the Console.

Figure 30: New output is generated

Common Errors

When developing the application, some errors could occur, such as the Errors shown in Eclipse. The first two errors indicate that the g++ and gcc compilers are not found and the subsequent errors also indicate that the C++ syntax could not be resolved because the compilers are not found. The errors may be fixed by installing MinGW and adding the C:\MinGW\bin to PATH environment variable.

Figure 31: Common errors

In this tutorial, we introduced using C++ in Eclipse IDE for C/C++ Developers.

This article was originally published on Wednesday Nov 30th 2016
Mobile Site | Full Site