This quick guide explains how to compile and run C / C ++ programs on the GNU / Linux operating system.
If you are a student or new Linux user who is moving from the Microsoft platform, then you may be wondering how to run C or C ++ programs on a Linux distribution. We need to understand that compiling and running code on Linux platforms is slightly different from Windows.
Installing the necessary tools
As you probably already understand, in order to run the code, you need to install the necessary tools and compilers to work. Below we describe how to install all development tools on Linux.
For work and testing, we must have a server running Linux. The best option is a VPS. Depending on the geography of the project, two countries are usually chosen for servers – VPS USA and VPS Russia.
In this quick tutorial, we will discuss how to install development tools on Linux distributions such as Arch Linux, CentOS, RHEL, Fedora, Debian, Ubuntu, openSUSE, and more.
These development tools include all the necessary applications such as the GNU GCC C / C ++ compilers, make, debuggers, man pages, and others that are needed to compile and build new software and packages.
The developer tools can be installed individually or all at once. We’re going to install everything at once to make it much easier for us to work with.
Installation on Arch Linux
To install development tools on Arch Linux and its distributions such as Antergos, Manjaro Linux, simply run:
$ sudo pacman -Syyu
$ sudo pacman -S base-devel
The above command will install the following packages on your Arch based systems:
- autoconf
- automake
- binutils
- bison
- fakeroot
- file
- findutils
- flex
- gawk
- gcc
- gettext
- grep
- groff
- gzip
- libtool
- m4
- make
- pacman
- patch
- pkg-config
- sed
- sudo
- texinfo
- util-linux
- which
Just hit ENTER to install them all.
If you want to install a package to a specific group of packages, just enter its number and press ENTER to proceed with the installation.
Installing development tools on RHEL, CentOS
To install development tools on Fedora, RHEL and its clones such as CentOS, Scientific Linux, run the following commands as user root:
$ yum update
$ yum groupinstall "Development Tools"
The above command will install all the required developer tools, for example:
- autoconf
- automake
- bison
- byacc
- cscope
- ctags
- diffstat
- doxygen
- elfutils
- flex
- gcc / gcc-c ++ / gcc-gfortran
- git
- indent
- intltool
- libtool
- patch
- patchutils
- rcs
- subversion
- swig
Installing development tools on Debian, Ubuntu and distributions
To install the required developer tools on DEB based systems, run:
$ sudo apt-get update
$ sudo apt-get install build-essential
This command will provide all the necessary packages to set up a development environment on Debian, Ubuntu and its distributions.
- binutils
- cpp
- gcc-5-locales
- g ++ – multilib
- g ++ – 5-multilib
- gcc-5-doc
- gcc-multilib
- autoconf
- automake
- libtool
- flex
- bison
- gdb
- gcc-doc
- gcc-5-multilib
- and many.
You now have the development tools you need to create software on Linux.
Mangi script
If you don’t like the method of installing development kits above, there is also a script called “manga script” (mangi) available to easily set up the development environment on DEB systems such as Ubuntu, Linux Mint and other Ubuntu derivatives.
After a fresh install of Ubuntu, grab this script from the GitHub repository, make it executable, and start installing all the necessary tools and packages to set up a complete development environment. You don’t need to install the tools one by one.
This script will install the following development environments and tools on your Linux system:
- Node.js
- NVM
- NPM
- Nodemon
- MongoDB
- Forever
- git
- grunt
- bower
- vim
- Maven
- Loopback
- curl
- python
- jre / jdk
- gimp
- zip unzip and rar tools
- filezilla
- tlp
- erlang
- xpad sticky notes
- cpu checker
- kvm acceleration
- Caliber Ebook Reader (I often use it to read programming books
- Dict – Ubuntu Dictionary Database and Client (CLI based)
Install the following first:
$ sudo apt-get install wget unzip
Download the manga script using the command:
$ wget https://github.com/sojharo/mangi-script/archive/master.zip
Extract the downloaded archive:
$ unzip master.zip
The above command will unpack the zip file into a folder called mangi-script-master in your current working directory. Change to the directory and make the script executable using the following commands:
$ cd mangi-script-master/
$ chmod a+x my_ubuntu_setup.sh
Finally, run the script with the command:
$ sudo ./my_ubuntu_setup.sh
Please keep in mind that this script is not fully automated. You need to answer a series of Yes / No questions to install all development tools.
Installing development tools on openSUSE / SUSE
To set up your development environment in openSUSE and SUSE enterprise, run the following commands as root user:
$ zypper refresh
$ zypper update
$ zypper install -t pattern devel_C_C++
Checking the installation
Now let’s check if the development tools were installed or not. To do this, run:
$ gcc -v
$ make -v
As you can see from the above output, the development tools were installed successfully. Now you can start developing your applications.
Setting up the development environment
A script called ‘mangi’ will help you set up a complete environment on Ubuntu based systems.
Once again, after installing the necessary development tools, you can check them using one of the following commands:
$ whereis gcc
$ which gcc
$ gcc -v
These commands will show the installation path and compiler version gcc…
Compiling and running C, C ++ programs
First, let’s see how to compile and run a simple C program.
Compiling and running C programs
Write your code / program in your favorite CLI / GUI editor.
I am going to write my C program using an editor nano…
$ nano ostechnix.c
Note… You need to use the .c extension for C programs or .cpp for C ++ programs.
Copy / paste the following code:
#include <stdio.h>
int main()
{
printf("Welcome To OSTechNix!");
return 0;
}
Press Ctrl + O and Ctrl + X to save and exit the file.
To compile the program, run:
$ gcc ostechnix.c -o ostechnix
Or:
$ cc ostechnix.c -o ostechnix
If there are syntax or semantic errors in your code / program, they will be displayed. First you need to fix them in order to move on. If there is no error, then the compiler will successfully generate an executable file ostechnix in the current working directory.
Finally, run the program with the command:
$ ./ostechnix
You will see output as shown below:
Welcome To OSTechNix!
To compile multiple source files (for example source1 and source2) into an executable file, run:
$ gcc source1.c source2.c -o executable
To resolve warnings, you need to debug the output symbols:
$ gcc source.c -Wall -Og -o executable
Compile the source code into an assembler instruction:
$ gcc -S source.c
Compile source code without linking:
$ gcc -c source.c
The above command will create an executable called source.o…
If your program contains math functions:
$ gcc source.c -o executable -lm
For more information, see the man pages (man pages).
$ man gcc
Compiling and Running C ++ Programs
Write your C ++ program in any editor of your choice and save it with the .cpp extension.
An example of a simple C ++ program:
$ cat ostechnix.cpp
Program:
#include <iostream>
int main()
{
std::cout << "Welcome To OSTechNix!" << std::endl;
return 0;
}
To compile this C ++ program on Linux, just run:
$ g++ ostechnix.cpp -o ostechnix
If there were no errors, then you can run this C ++ program under Linux using the command:
$ ./ostechnix
Will output:
Welcome To OSTechNix!
Alternatively, we can compile the above C ++ program using the command “make“as shown below.
$ make ostechnix
You noticed? I have not used the .cpp extension in the above command to compile the program. There is no need to use the extension to compile C ++ programs using the make command.
Run using the command:
$ ./ostechnix
Welcome To OSTechNix!
See the man pages for more details.
$ man g++
Hope the article helped.