In this tutorial, we will see how to install, setup, and use Eclipse for C++ development:
Eclipse is a widely used IDE primarily for Java development. Eclipse is also used for C and C++ development as well as PHP among the other programming languages.
Eclipse IDE is written in Java. It mainly consists of a base ‘workspace’ and a plug-in system so that we can add more plugins and extend the functionality of the IDE.
Eclipse works on all the major platforms including Windows, Mac OS & Linux, and boasts of powerful features that can be used to develop full-fledged projects.
What You Will Learn:
Eclipse For C++
The development environment for Eclipse includes:
- Eclipse Java Development Tools (JDT) for Java and Scala.
- Eclipse C/C++ Development Tools (CDT) for C/C++.
- Eclipse PHP Development Tools (PDT) for PHP.
Official Website: Eclipse
In this tutorial, we will explore the features of the Eclipse IDE with respect to C/C++ development (Eclipse CDT) and also discuss all the steps to setup eclipse on our computer to begin development.
Features Of Eclipse IDE
Enlisted below are the features of Eclipse IDE:
- Almost everything in Eclipse is a plugin.
- We can extend the functionality of Eclipse IDE by adding plugins to the IDE, maybe for additional programming language or version control system or UML.
- Eclipse has a wonderful user interface with drag and drop facility for UI designing.
- Supports project development and administered framework for different toolchains, classic make framework, and source navigation.
- Supports various source knowledge tools like folding and hyperlink navigation, grading, macro definition browser, code editing with syntax highlighting.
- Provides excellent visual code debugging tool to debug the code.
Install And Configure Eclipse For C++
In order to install and configure Eclipse IDE for C/C++ development, firstly, we need to make sure that we have an appropriate GCC compiler on our machine.
Please follow the following steps to install and configure Eclipse IDE for C/C++.
Step 1: Install GCC Compiler
Eclipse CDT uses C/C++ Compiler. Hence before we can start using Eclipse CDT for C/C++ development, we need to have a proper GCC compiler on our system. We can either have ‘MinGW’ or ‘Cygwin’ compiler on our machine that will be used by eclipse.
We will not go into the details of the installation of these compilers, but we will provide the appropriate links that will be useful to our readers.
Step 2: Install Eclipse C/C++ Development Tool (CDT)
There are two ways of installing Eclipse CDT based on whether you already have an Eclipse IDE on your system or not, depending on whether you have previously installed an Eclipse:
If you already have Eclipse JDT (Eclipse for Java) or any other Eclipse environment on your system, then you can add a CDT plug-in to this environment.
Given below are the steps to add CDT plug-in to the existing Eclipse environment:
#1) Launch Eclipse.exe
When you launch Eclipse for the first time you have to create a workspace that will hold all your projects. After that every time you open Eclipse IDE, you will be shown a dialog to select the workspace.
In the above dialog, you can either create a new workspace or select an existing workspace, click ok and the IDE will open.
#2) Click Help =>Install New Software. In the “Available Software” dialog, enter “Kepler – http://download.eclipse.org/releases/kepler” (or Juno for Eclipse 4.2; or Helios for Eclipse 3.7) in the “Work With” field or pull down the dropdown menu and select the above link.
#3) In the “Name” field, expand “Programming Language” and check the option “C/C++ Development Tools”.
#4) Click Next => Finish.
This sequence of steps is shown in the below screenshot:
Once the plug-in is installed, we are ready to begin C/C++ development using Eclipse IDE.
If there is no Eclipse IDE present on the system, then we can directly install Eclipse CDT by downloading the Eclipse CDT package.
There is no installation sequence as such, you just have to unzip the contents of the downloaded package and then run “Eclipse.exe” and you are ready for C/C++ development using the Eclipse IDE.
Step 3: Configuring Eclipse IDE
It is not required to specifically do any configuration for Eclipse CDT as long as you have MinGW or Cygwin binaries updated in the PATH environment variable.
CDT searches the PATH and discovers the C/C++ Compilers on its own.
Now you are ready to use the Eclipse.
C++ Development Using Eclipse IDE
To test the environment you just configured, open Eclipse.exe.
Choose File => New => C++ Project and the below screen will appear.
Here you can specify the name of the project. You can select an Empty project or a sample “Hello World” application project. The compilers present on your system are listed under “ToolChains”. You can select the appropriate compiler and then click Next.
Another way to select the compiler and set other properties for the just created project is to right-click on the project name in the project explorer and select “Properties”.
You will be presented with the following screen.
In this dialog, we can set various properties for the selected project.
Once the project is ready, we can add a file with the .cpp extension and write a code. Once you have written the desired code, it’s time to compile and build the code.
Note that you can have more than one code file in the project. You can also create a C++ class inside the project.
Build And Execute Projects In Eclipse
We can build the project by right-clicking the project name in the Project Explorer and select “Build Project”.
Once the build is successful, run or execute the project. For this, right-click the project name on the Project Explorer and click “Run as”. Then select “Local C/C++ Application”. This runs your application.
Debugging An Application In Eclipse
If you get the desired output when you run the project, then you can say that the project is successful. But if you don’t get the desired results, then you might have to debug your application.
Let’s see how to debug an application in Eclipse.
To debug a project, we have to perform the following steps:
#1) Set a Breakpoint
By setting up a breakpoint, you can suspend the execution of the program. This will allow you to examine the program step by step and also watch the intermediate values of variables and flow of execution so that you can find out the problem in your code.
It is normally a good practice to set the breakpoint in the main function as it’s the starting point for a C++ program. To set a breakpoint, you can double click on the left panel of the code file against the line of code for which you want a breakpoint.
Another way is to click “Ctrl+Shift+B” by placing the cursor on the line of code for which the breakpoint is needed.
The red arrow shows the line for which the breakpoint is set. It is denoted by a circle on the left-hand pane.
#2) Start Eclipse Debugger
Once the breakpoint is set, you can start debugger by right-clicking (or Run option in the menu) the project name and select “Debug As=> Local C/C++ Application”. On doing this your execution will pause at the line at which the breakpoint is set.
#3) Step Over and Watch Variables and Output
After starting the debug, you can “Step Over” each line of code and examine the variable values by hovering your mouse over that variable.
This process of stepping through each line of code is the ultimate method to debug your program.
#4) Debug operations => Run-to-line resume, terminate
These are all the operations that you can perform with debugging. Run-to-line will continue the program execution up to the line where the cursor is placed.
Resume continues the program execution up to the next breakpoint or till the end of the program. Terminate -terminates the debugging session.
The below screenshot shows the debug toolbar and the operations that we discussed.
#5) Switch back to the development perspective.
Click the C/C++ icon shown in the above screenshot to switch back to the project for further programming.
Readers can explore the other debugger features like step-into (wherein we can go inside any function and debug it), modify the value of the variable being watched, etc.
In this tutorial, we have seen the features, installation, configuration, and development using Eclipse CDT IDE. Although Eclipse IDE is primarily used for Java development, we can also use it for development using other programming languages like C/C++, PHP, Perl, Python to name a few.
Eclipse has a graphical debugger and thus debugging of applications becomes easier. We can develop too many advanced applications using Eclipse IDE as it’s an IDE that is easy to use.