Compile And Run C/C++ Code In Linux

Watch out! This tutorial is over 8 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.
Tutorial Difficulty Level    

We are quite often asked by students “How can I compile a C or C++ program on Linux operating systems using bash Terminal application?” Today we will show you how!

To compile a C or C++ program on any Linux distro such as Ubuntu, Red Hat, Fedora, Debian and other Linux distro you need to install:

  • GNU C and C++ compiler collection
  • Development tools
  • Development libraries
  • IDE or text editor to write programs

Step #1: Install C/C++ compiler and related tools

If you are using Debian or Ubuntu Linux, type the following apt-get command to install GNU c/c++ compiler:

$ sudo apt-get update
$ sudo apt-get install build-essential manpages-dev

Step #2: Verify installation

Type the following command to display the version number and location of the compiler on Linux:

$ whereis gcc
$ which gcc
$ gcc --version

Sample output:

itlc@debian:~$ whereis gcc
gcc: /usr/bin/gcc /usr/lib/gcc
itlc@debian:~$ which gcc
/usr/bin/gcc
itlc@debian:~$ gcc --version
gcc (Debian 4.9.2-10) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

How to Compile and Run C/C++ program on Linux

Create a file called demo.c using a text editor such as vi, emacs or nano:

#include<stdio.h>
/* demo.c:  My first C program on a Linux */
int main(void)
{
 printf("Hello! This is a test prgoram.\n");
 return 0;
}

How do I compile the program on Linux?

Use any one of the following syntax to compile the program called demo.c:

cc program-source-code.c -o executable-file-name

or

gcc program-source-code.c -o executable-file-name

or

## assuming demo.c exists in the current directory ##
make demo

If there is no error in your code or C program then the compiler will successfully create an executable file called demo in the current directory, otherwise you need fix the code. To verify this, type:

$ ls -l demo*

How do I run or execute the program called demo on Linux?

Simply type the the program name:

$ ./demo

Compiling and running a simple C++ program

Create a program called demo2.C as follows:

#include "iostream"
// demo2.C - Sample C++ prgoram 
int main(void) 
{
    std::cout << "Hello! This is a C++ program.\n";
    return 0;
}

To compile this program, enter:

g++ demo2.C -o demo2
## or use the following syntax ##
make demo2

To run this program, type:

./demo2

How do I generate optimized code on a Linux machine?

The syntax is as follows C compiler:

cc -O input.c -o executable

The syntax is as follows C++ compiler:

g++ -O -Wall input.C -o executable

How do I compile a program with multiple source files?

The syntax is as follows if the source code is in several files (such as light.c, sky.c, fireworks.c):

cc light.c sky.c fireworks.c -o executable

C++ syntax is as follows if the source code is in several files:

g++ ac.C bc.C file3.C -o my-program-name