Getting Started with 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.

Start01

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.

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

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

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

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

Start14
Figure 14: Specifying the Cross compiler settings

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

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

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

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

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

Start19
Figure 19: Running the binary version of the file

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

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

Start21
Figure 21: Clicking Apply

Click Run to run the configuration.

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

Start23
Figure 23: Selecting the source code

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

Start24
Figure 24: The compiled application is run

The “!!!Hello World!!!” message gets output in the Console.

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

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

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

Save the updated application with File>Save All.

Start28
Figure 28: Saving all the files

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

Start29
Figure 29: Selecting Run

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

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

Start31
Figure 31: Common errors

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

More by Author

Must Read