How To 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:
Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements GNU C/C++ compiler
Time 10m
  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

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

Compiling and running a simple C++ program

Create a program called demo2.C as follows:

#include "iostream"
// demo2.C - Sample C++ program 
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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 38 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
38 comments… add one
  • pavi Apr 28, 2014 @ 15:10

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

  • Amani Musomba May 24, 2014 @ 13:58

    Very nice article…..

  • Ravi Jun 18, 2014 @ 16:36

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

  • Ravi Jun 18, 2014 @ 16:56

    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 Jul 9, 2014 @ 13:44

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

    • GK Jan 13, 2016 @ 2:23

      For the sake of supplying an example, let’s say you want to use the cosine function. This is supplied in the Linux math library. The cosine function is called ‘cos()’. Similarly, the sine function is called ‘sin()’.

      First, to find information about how to use them, type “man cos” in a terminal session. This gives you the manual page for the cosine function. The output from ‘man’ may vary for your system, but it likely tells you three things: 1. first, include the math.h header, 2. cos() takes a ‘double’ as its argument and it returns a double as its output, 3. to build your program, tell the C compiler to include the math library (-lm).

      Here’s a sample program that does all of this:

      #include <stdio.h>   /* provides printf() */
      #include <math.h>  /* provides cos() */
      /* cosine.c: A sample C program on Linux that calls the cosine function. */
      /*   use "cc cosine.c -o cosine.c -lm"   */
      int main( void )
        printf( "Hello! This program calls the linux cosine function." );
        double intput = 0;
        double output = cos( 0 );  /* get the cosine of zero */
        printf( "The cosine of %f is %f\n", input, output );
        return 0;
  • thaidn Aug 15, 2014 @ 3:16

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

  • kameswar Sep 6, 2014 @ 13:38

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

  • Jitendra singh Oct 1, 2014 @ 4:57

    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 Aug 12, 2015 @ 4:25

      very nice to your step.

  • Amber Michaud Dec 7, 2014 @ 15:40

    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 Dec 12, 2014 @ 11:56

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

    thank you

  • meowski Jan 8, 2015 @ 19:49


    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 Jan 10, 2015 @ 9:20

    Thanks Bro.

  • craaaaaaaig Jan 11, 2015 @ 6:21

    Very helpful. Thank you…

  • Rakesh Patni Feb 3, 2015 @ 16:51

    Really helpful. Many Thanks

  • yogesh Feb 10, 2015 @ 14:35

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

    • 🐧 nixCraft Feb 10, 2015 @ 16:44

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

      yum groupinstall 'Development Tools'


      sudo yum groupinstall 'Development Tools'
  • brajeshtiwary Feb 22, 2015 @ 22:02

    so thank

  • Fernando Mar 22, 2015 @ 3:21

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

  • pooyan Jun 11, 2015 @ 15:42

    thank you so much for your guidance

  • Palartu Jun 16, 2015 @ 19:27

    Thanky you very much for this description!

  • Chris S Jul 6, 2015 @ 15:05

    Thank you!

  • Md.Ibrahim Aug 4, 2015 @ 8:19

    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

    • coder Jan 12, 2016 @ 18:04

      You have a syntax error on line 3 of your file: demo2.c
      Make sure you have typed it in correctly.

    • GK Jan 13, 2016 @ 2:26

      You need to look carefully at the contents of your file “demo2.c”. From the error message you provided, it looks like there is something wrong on line 3 or otherwise near line 3. Look for this “â/â” charactger and remove it. Then retry the g++ command. Good luck to you!

  • Dileep Aug 7, 2015 @ 10:54

    Thanking you, awesome information

  • Sprite Aug 26, 2015 @ 5:41

    Very useful tutorial, thanks!

  • Midi Sep 23, 2015 @ 17:05

    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 Oct 3, 2015 @ 18:36

    thank you

  • don bosco Jan 8, 2016 @ 11:09

    Thank you very much for this page Cyberciti’s team
    My age is above 30 and i work a waiter .I learned very thing about computer from the INTERNET. I started using Linux from 2013 and this is the only website i found tutorials of g++ explained from the basics .

  • khush Mar 21, 2016 @ 8:23

    i did the same for c program
    steps upto gcc progname.c -o prgnam work fine
    but when i type ./executablefilename error with Bsh: ./hello permission denied appears.
    i am not getting why is this happening.
    I installed ubuntu alongside Windows two days ago and using ubuntu for programming for the first time.

  • sumit panwar Jul 10, 2016 @ 10:12

    thanks, this use full for me ……………..

  • sumit panwar Jul 10, 2016 @ 10:16

    what is diffrince b/w window and linux related to c .,, like if we creat a program in turbo c ,like if stetment.. are some deffrence to between both li & win////

  • ChainaT Oct 11, 2016 @ 4:25

    Thank you so much. I have been tryin to find the place to understand how to compile C++ file in Linux. Yours is the descriptive yet easy to read and follow. Thank so much!!!

  • ADHI P Nov 7, 2016 @ 2:54

    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

  • Tommy Nov 9, 2016 @ 2:28

    when i want to display the output. it prints the printf content, but when i use scanner, i dont get an option to input data. Help??

  • RAJDEO JHA Sep 22, 2020 @ 13:26

    there are data base in binary system is file,
    the c processing to work a application a file,
    there are application to placement value a
    prework value application to place
    sub+ calculus= Inter/value*work/pkace( equation 1)
    =|state \vertical
    =|state v3*unit
    even number=operator/operation (equation2)

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @