How To Setup A C++ Development Environment In Ubuntu Linux

 

Brief: This tutorial teaches you how to setup a C++ development environment in Ubuntu Linux using Eclipse IDE.

I have been requested more than once about writing an easy to follow tutorial to run C++ program in Linux. In this guide, I’ll show you:

  • how to compile and run C++ programs in Ubuntu Linux
  • how to setup Eclipse for C++ development in Ubuntu Linux

Do note that I am using Ubuntu Linux while writing this article but same steps are valid for other Linux distributions based on Ubuntu such as Linux Mint, elementary OS etc.

Install build-essential

If you want to do coding in Ubuntu Linux, you must install build-essential package. It consists of various software that you will need to compile programs, including gcc and g++ compilers.

Normally, build-essential should already be installed on your system. But to make it sure, run the command below:

sudo apt-get install build-essential

Suggested Read4 Best Modern Open Source Code Editors For Linux

Method 1: Compile and run C++ program in Ubuntu Linux

Once you have the build-essential installed, you are ready to code in C++. I believe that you already know how to code in C++, even a little bit. Our main aim is to see how to compile and run C++ programs in Linux.

Let’s take an example of Swap program C++ which I wrote in a file named swap.cpp. The content of this file is the following:

unnamed-file-270-1988926

You can save the program wherever you want.

Compile C++ code in Linux:

To compile the program, go to the directory where you have saved the cpp file and use the command in the following format:

g++ -o swap swap.cpp

Basically, with -o option, your are telling the compiler to generate the executable code in file swap. If you don’t do that, it will be defaulted to a.out file which is not a good programming practice.

Run C++ code in Linux:

Once you have compiled the code, you’ll get the executable file. You just need to run it in the following manner:

./swap

This will run your code.

You can refer to this gif for a better demonstration of running a C++ program in Ubuntu Linux.

unnamed-file-48-4017398

Method 2: Setup Eclipse for C++ programming in Ubuntu Linux

That was the basic way of running a C++ program in Linux. But if you are working on a C++ project, building and running individual files would be a nightmare.

This is where Integrated Development Environment (IDE) comes in picture. One can argue a lot about best IDE for Linux but if you ask for my advice, I’ll say go ahead with Eclipse. This is the best IDE for C++ development in my opinion. Did I mention that it is also open source?

Suggested Read7 Best Notepad++ Alternatives For Linux

Install Eclipse in Ubuntu based Linux distributions

For Ubuntu Linux, you can simply click on the link below to install Eclipse from Ubuntu Software Center.

Get Eclipse for Ubuntu

Alternatively, you can install it using apt-get commands in the terminal:

sudo apt-get install eclipse

Install Eclipse C++ Development Tooling (CDT) Plugin

Once you have it installed, it is time to prepare it for C++ development. By default, Eclipse is configured for Java development.

To configure it for C++ development, we need to install a plugin called C++ Development Tooling (CDT). To install CDT:

Step 1:

In the Eclipse menu, go to Help and then select Install New Software.

c-plus-plus-development-eclipse-plugin-5209332

Step 2:

Next, click on the “Available Software Sites” link.

c-plus-plus-development-eclipse-plugin-1-800x682-8509649

Step 3:

In the next step, search for CDT and check the box to select it for installation. Click OK afterward.

unnamed-file-271-3760106

Step 4:

In here, select the newly added source from the drop down. It will now show you the option for C++ CDT. Just select C++ Development Tools here.

c-plus-plus-development-eclipse-plugin-3-800x682-5771298

A few click on the Next button.

c-plus-plus-development-eclipse-plugin-4-800x682-5463309

Accept the terms of course.

c-plus-plus-development-eclipse-plugin-5-800x682-5718804

It will get the software from the repository and install it.

unnamed-file-272-4266995

Once the installation is finished, you need to restart Eclipse.

unnamed-file-273-1853973

Suggested ReadHow To Change Eclipse Color Theme In Linux or Windows

Compile and run C++ program with Eclipse CDT

You’ll see the information about C++ Plugin at the next start.

c-plus-plus-development-eclipse-plugin-8-2800937

You can now import or create C++ projects.

unnamed-file-274-7487483

unnamed-file-275-2022702

Once you have everything ready, you can compile the C++ project and run it:

c-plus-plus-development-eclipse-plugin-11-800x468-3357584

That’s all you need to get started with C++ development in Ubuntu Linux. I hope you found this article useful. Questions and suggestions are welcomed.

Source