- How to Run C/C++ Programs in Linux [Terminal & Eclipse]
- Prerequisite: Install build-essential
- Method 1: Compile and run C++ program in Linux terminal
- Compile C++ code in the Linux terminal
- Run C++ code in the Linux terminal
- Method 2: Setup Eclipse for C++ programming in Ubuntu Linux
- Installing Software on Linux¶
- Debian, Ubuntu: APT¶
- Fedora, Red Hat: yum ¶
- Mandriva: urpm ¶
- Tar Balls¶
- Other Systems¶
How to Run C/C++ Programs in Linux [Terminal & Eclipse]
I have been requested more than once to write an easy-to-follow tutorial to run C++ programs in Linux. In this guide, I’ll discuss:
The process is pretty much similar to running C program in Linux.
Do note that I am using Ubuntu Linux while writing this article but the same steps are valid for other Linux distributions based on Ubuntu, such as Linux Mint, elementary OS, etc.
Prerequisite: Install build-essential
If you want to do coding in Ubuntu Linux, you should install build-essential package. It consists of various software that you will need to compile programs, including gcc and g++ compilers.
You may install gcc on Ubuntu and other distributions separately as well but the build-essential has additional tools that you may need.
Normally, build-essential should already be installed on your system. But to make sure, run the command below:
sudo apt install build-essential
Method 1: Compile and run C++ program in Linux terminal
Once the build-essential is 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 the terminal.
Let’s take an example of the swap program which I wrote in a file named swap.cpp. The content of this file is the following:
You can save the program wherever you want.
Compile C++ code in the Linux terminal
To compile the program, go to the directory where you have saved the cpp file and use the command in the following format:
Basically, with the -o option, you are telling the compiler to generate the executable code in file swap. If you don’t do that, it will default to a.out file, which is not a good programming practice.
Run C++ code in the Linux terminal
Once you have compiled the code, you’ll get the executable file. You just need to run it in the following manner:
You can refer to this gif for a better demonstration of running a C++ program in Ubuntu Linux.
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 the 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?
Recommended Read:
Installing Software on Linux¶
One of the most difficult things to get used to in the Linux world is installing new software packages. In the world of Windows, every program comes with a Setup.exe program that asks you some very easy questions and takes care of the job for you. While Linux software can be almost that easy to install, you will sometimes find software that seems to fight every step of the way. I can’t cover all the problems you might run into, but I’ll try to give you the basics and a few pointers to help get you over the rough spots.
Software tends to come in “packages”. In the Windows world a package is a Setup.exe or a program.zip file. On a Mac a package is a program.dmg or a program.sit file. In the Linux world, there are several kinds of packages, and each distribution has its own preferred package format.
The standard Linux package format (according to the Linux Standard Base) is RPM. RPM is a packaging system originally developed by Red Hat and widely used in the Linux community. Distributions using it include Fedora, Mandriva, Red Hat (naturally), and SUSE. An RPM package file normally will be named something like program-version-other.rpm
Another popular package format is DEB, the Debian software package. Debian packages and the Advanced Packaging Tool (APT) were the first to introduce several advanced features that are now common, such as automatic dependency resolution and signed packages. Debian packages are used by Debian GNU/Linux (naturally), and distributions based on it, including Ubuntu, Knoppix, and Mepis. A Debian package file normally will be named something like program-version-other.deb
Remember, you will need to become SuperUser to install software.
Debian, Ubuntu: APT¶
There is a broad array of tools for working with DEB packages, but the one you will commonly use is apt-get , arguably the easiest of Linux package management tools. apt-get is so easy because it not only keeps track of what packages are installed, but also what other packages are available. It will even download them from the Internet for you (if properly configured).
To remove software is just as easy.
Although the repositories that contain installable packages might live on the Internet or on a disc somewhere, APT keeps a local database on your hard drive with a list of all available packages and where to find them. This database needs to be explicitly updated. To update the APT database:
A common idiom is to update your package database, and then upgrade all the packages that have patches or security updates to install. The following command will do this all at once.
apt-get update; apt-get upgrade
For a more indepth apt-get tutorial and other resources, see Managing Software with APT and dpkg .
Fedora, Red Hat: yum ¶
yum does for RPM packages roughly what apt-get does for Debian packages. Like apt-get , yum can download and install packages from a configured repository.
To remove software is just as easy.
yum does not keep a local copy of your package database by default, so normally there is no need to update it. To install all available security patches and bug fixes, use this command:
You can also explicitly update a single package with:
For a more indepth yum tutorial and other resources, see Managing Software with yum and rpm .
Mandriva: urpm ¶
Mandriva Linux (formerly Mandrake and Connectiva) has a toolset similar to APT called urpmi . To install software:
To update the local package database:
To install security updates and bug fixes:
For a more indepth yum tutorial and other resources, see Managing Software with urpm .
Tar Balls¶
No, this is not a naughty term! A tar ball is a (usually compressed) archive of files, similar to a Zip file on Windows or a Sit on the Mac. Tar balls come in files that end in .tar , .tar.gz , .tgz , or something along these lines. To unpack a tar ball, use this command.
The parameters are x to extract files, z to filter through gzip for decompression (leave this off if the file does not have a gz extension), v for verbose mode so you can tell what’s going on, f indicating there will be a filename to follow. You may want to create an alias called “untar” that feeds in these options if you have a hard time remembering command line options as I do.
This command will not install the software, it will only extract the archived files. It is your job then to find the README file or INSTALL file and read its instructions for installation. If the archive contains binaries there will usually be a setup script (often called install.sh ) that you must execute as SuperUser .
Very often, software delivered in tar balls is not in executable form, but in source code, which must first be compiled before it can be installed. For more details on this, see Installing Software from Source Code .
Other Systems¶
Some other Linux distributions have their own way of managing packages, notably SUSE. SUSE uses RPM as its native package format, but has its own high level tool to manage system software installation.
SUSE Linux uses a tool called yast (which allegedly is an acronym for Yet Another Setup Tool) to perform all kinds of system administration tasks, including installing software. Having no experience with it, I cannot give you more details. man yast for help.