≡ Menu

HowTo: Compile And Run a C/C++ Code In Linux

I am a new Linux user and student who used to write C or C++ programs on MS-Windows. Now, I am using Ubuntu Linux. How can I compile a C or C++ program on Linux operating systems using bash Terminal application?

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:

  1. GNU C and C++ compiler collection
  2. Development tools
  3. Development libraries
  4. IDE or text editor to write programs

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

If you are using Fedora, Red Hat, CentOS, or Scientific Linux, use the following yum command to install GNU c/c++ compiler:
# yum groupinstall 'Development 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 outputs:

Fig. 01: GNU C/C++ compilers on Linux

Fig. 01: GNU C/C++ compilers on Linux

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 joe:

/* 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


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


## assuming that executable-file-name.c exists ##
make executable-file-name

In this example, compile demo.c, enter:

cc     demo.c   -o demo


## 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
$ /path/to/demo
Samples session:

Animated gif 01: Compile and run C and C++ program demo

Animated gif 01: Compile and run C and C++ program 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:


How do I generate symbolic information for gdb and warning messages?

The syntax is as follows C compiler:
cc -g -Wall input.c -o executable
The syntax is as follows C++ compiler:
g++ -g -Wall input.C -o executable

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 C program that uses math functions?

The syntax is as follows when need pass the -lm option with gcc to link with the math libraries:
cc myth1.c -o executable -lm

How do I compile a C++ program that uses Xlib graphics functions?

The syntax is as follows when need pass the -lX11 option with gcc to link with the Xlib libraries:
g++ fireworks.C -o executable -lX11

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

See gcc(1) Linux and Unix man page for more information.

{ 27 comments… add one }

  • pavi April 28, 2014, 3:10 pm

    thank you so much ur solution gave a relief…
    it made my gcc command to work

  • Amani Musomba May 24, 2014, 1:58 pm

    Very nice article…..

  • Ravi June 18, 2014, 4:36 pm

    In Fig. 01, you did “whereis” twice. Shouldn’t it be “which” the second time? Thanks for the tut though. Big fan!

  • Ravi June 18, 2014, 4:56 pm

    Another mistake, please change the following comment:
    ## assuming that executable-file-name.c exists ##
    ## assuming that program-source-code.c exists in the current directory ##

  • Eliphaz July 9, 2014, 1:44 pm

    how to compile a program that use math functions and other things?

  • thaidn August 15, 2014, 3:16 am

    Love it!
    Thank you. I have a trouble in doing step 1 and 2. But they are fixed.

  • kameswar September 6, 2014, 1:38 pm

    thank u ,
    need pdf of the commands guide to access the c/c++/java.

  • Jitendra singh October 1, 2014, 4:57 am

    to compile and run a c++ program in ubuntu follow these simple steps:
    1 open terminal window.
    2 type “gedit” .
    3 A gedit window will appear whereyou can write your program.
    4 save your program as “filename.cpp” on desktop, “.cpp” is compulsory.
    5 open terminal again and type “cd Desktop”.
    6 In second line type “g++ filename.cpp”.
    7 Type “./a.out”.

    • j s rathore August 12, 2015, 4:25 am

      very nice to your step.

  • Amber Michaud December 7, 2014, 3:40 pm

    Thanks! This article really helped me to find the GNU compiler in a Linux Operating System and showed me how to compile a C program.

  • prasanta mangar December 12, 2014, 11:56 am

    dear sir,
    what is the procedure to run .cpp program in linux distro debian 5 ?

    thank you

  • meowski January 8, 2015, 7:49 pm


    just about to get around to learning c along with teaching my sons it. i had no idea where to start, the first page i checked is a bumper bonanza.

    thanks for sharing the information.

  • Amandeep January 10, 2015, 9:20 am

    Thanks Bro.

  • craaaaaaaig January 11, 2015, 6:21 am

    Very helpful. Thank you…

  • Rakesh Patni February 3, 2015, 4:51 pm

    Really helpful. Many Thanks

  • yogesh February 10, 2015, 2:35 pm

    # yum groupinstall ‘Development Tools’
    im using these command but not install the gcc ….
    [samrat@localhost ~]$
    [samrat@localhost ~]$ # yum groupinstall ‘Development Tools’
    [samrat@localhost ~]$

    • nixCraft February 10, 2015, 4:44 pm

      First, use sudo or su to become the root and type:

      yum groupinstall 'Development Tools'


      sudo yum groupinstall 'Development Tools'
  • brajeshtiwary February 22, 2015, 10:02 pm

    so thank

  • Fernando March 22, 2015, 3:21 am

    Life saver for new Linux/C++ developers. Thank you!!!!

  • pooyan June 11, 2015, 3:42 pm

    thank you so much for your guidance

  • Palartu June 16, 2015, 7:27 pm

    Thanky you very much for this description!

  • Chris S July 6, 2015, 3:05 pm

    Thank you!

  • Md.Ibrahim August 4, 2015, 8:19 am

    Hello Everyone,
    I am Learning C++ in Linux and I am Getting an ERROR for the command or To compile this program as follow:
    # g++ demo2.c -o demo2
    demo2.c:3: error: expected unqualified-id before â/â token

    Please give Solution for this.
    Thanks in Advance

  • Dileep August 7, 2015, 10:54 am

    Thanking you, awesome information

  • Sprite August 26, 2015, 5:41 am

    Very useful tutorial, thanks!

  • Midi September 23, 2015, 5:05 pm

    I have created a c program in which i have to display the contents of file but each time when i run the program it display the error that file can’t be opened the no such file exits.
    i don’t know how to give the path of that text file in my c program i have to run this program on linux. please someone help me as soon as possible.

  • shelli October 3, 2015, 6:36 pm

    thank you

Leave a Comment

   Tagged with: , , ,