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:

  1. autoconf
  2. automake
  3. binutils
  4. bison
  5. fakeroot
  6. file
  7. findutils
  8. flex
  9. gawk
  10. gcc
  11. gettext
  12. grep
  13. groff
  14. gzip
  15. libtool
  16. m4
  17. make
  18. pacman
  19. patch
  20. pkg-config
  21. sed
  22. sudo
  23. texinfo
  24. util-linux
  25. 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.

Write your first C ++ game

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:

  1. autoconf
  2. automake
  3. bison
  4. byacc
  5. cscope
  6. ctags
  7. diffstat
  8. doxygen
  9. elfutils
  10. flex
  11. gcc / gcc-c ++ / gcc-gfortran
  12. git
  13. indent
  14. intltool
  15. libtool
  16. patch
  17. patchutils
  18. rcs
  19. subversion
  20. 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.

  1. binutils
  2. cpp
  3. gcc-5-locales
  4. g ++ – multilib
  5. g ++ – 5-multilib
  6. gcc-5-doc
  7. gcc-multilib
  8. autoconf
  9. automake
  10. libtool
  11. flex
  12. bison
  13. gdb
  14. gcc-doc
  15. gcc-5-multilib
  16. 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:

  1. Node.js
  2. NVM
  3. NPM
  4. Nodemon
  5. MongoDB
  6. Forever
  7. git
  8. grunt
  9. bower
  10. vim
  11. Maven
  12. Loopback
  13. curl
  14. python
  15. jre / jdk
  16. gimp
  17. zip unzip and rar tools
  18. filezilla
  19. tlp
  20. erlang
  21. xpad sticky notes
  22. cpu checker
  23. kvm acceleration
  24. Caliber Ebook Reader (I often use it to read programming books
  25. 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.

No Comments
Comments to: Compiling and running C, C ++ programs on Linux

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

Good Reads

Worlwide

Trending

Advertiser

Login

Welcome to Typer

Brief and amiable onboarding is the first thing a new user sees in the theme.
Join Typer
Registration is closed.