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.

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.

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

Start02
Figure 2: Setting the PATH environment

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

Start03
Figure 3: Completing the download

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

Start04
Figure 4: Listing the Packages

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

Start05
Figure 5: Marking for installation

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

Start06
Figure 6: Selecting Installation>Apply Changes

Click Apply.

Start07
Figure 7: Applying the changes

The packages get downloaded and the scheduled changes get applied.

Start08
Figure 8: The packages are downloading

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

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

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.



About the Author

Deepak Vohra

Deepak Vohra is an O'Reilly technical reviewer, who reviewed the book WebLogic: The Definitive Guide. He's also a NuBean consultant and web developer, and is a Sun Certified Java 1.4 Programmer and Sun Certified Web Component Developer. Deepak has published two books on Docker.

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date