How to Compile and Run C/C++ Programs in Linux (Ubuntu)

Learn how to compile and run C/C++ programs in linux (ubuntu).

Linux is becoming very popular among the programmers. In my opinion you should also start doing programming in linux. I thought that I should share the process to write and run C/C++ programs on linux platform.

How to Compile and Run C/C++ Programs in Linux (Ubuntu)

1. First of all open terminal window, for this go to Applications > Accessories > Terminal, as shown in below image.

How to Compile and Run C/C++ Programs in Linux (Ubuntu)

2. To run C/C++ program you need to install the essential packages. For this enter the command given below in terminal window.

sudo apt-get install build-essential

Now it will ask for the admin password. When you enter the password the installation process will start.

Note: For installing packages you must be connected to internet. It will take few minutes to complete, depending upon your internet speed. Skip this step if packages are already installed.

3. Now its time to write and run the program. Below I have explained simple steps for both C and C++.

For C Program

1. Enter the command given below in terminal window to open text editor.

gedit prog.c

Here prog.c is the name of the program. After entering above code it will ask for password.

2. Now write your program, for your convenience I have given a sample program below, you can copy and paste it in gedit text editor.

3. Save and close the editor.

4. To compile the program enter the command given below in terminal window.

gcc prog.c -o prog

5. If there will be no error in the program then nothing will be shown. And if error occurs, it will be shown. In case you get an error you have to open the text editor again by repeating step 1 and remove it and again save and close the editor.

6. Enter the command given below to run the program.

./prog

7. Now you will see the output in the terminal window as shown in below image.

How to Compile and Run C/C++ Programs in Linux (Ubuntu)

For C++ Program

1. Enter the command given below in terminal window to open text editor.

gedit progcpp.cpp

2. Now write your program, I have given an example below.

3. Save and close the editor.

4. To compile the program enter the command given below in terminal window.

g++ progcpp.cpp -o progcpp

5. Enter the command given below to run the program.

./progcpp

Comment below if you have doubts or found anything incorrect in above tutorial.

17 thoughts on “How to Compile and Run C/C++ Programs in Linux (Ubuntu)

  1. Ogunyinka Joshua

    Dude this tutorial sucks! What the fuck are you teaching?
    (1) Your Ubuntu is version 8 or lesser. There's version 13.04(Raring, DVD)and 12.04(Precise Pangolin, CD) out there, they're free!
    (2) Really? "#include"! What is this? 1998?
    (3) Why not set -Wall flags and see what errors you'll encounter with those absurd codes?
    (4) "using namespace std" are for beginners. I may be developing apps in Qt and alias stdout with a QTextStream cout, what happens then? Always write that this way -> std::cout

    Reply
    1. glenford williams

      There is no need to bash anyone here, the main aim of this tutorial was to demonstrate how one would go about compiling c/c++ code in ubuntu linux not how to code like a pro….and ubuntu being outdated or not the principles are all the same

      Reply
    2. 7xCore

      What do you mean by "(2) Really? "#include"! What is this? 1998?" How else could you include any dependencies into that program?

      Reply
  2. Eduardo Luna

    That's ok, Neeraj! I wondered that you used to program embedded systems, like me. So, no matter if the notation is the 90 or more of them, if your current code is eficente and have an adequate performance. The point here, in my opinios is: Let free to use Linux. Thank you for the tutorial.

    Regards
    Eduardo Luna

    Reply
  3. Abhishek Mondal

    Yeah the command really help me to compile nd run the c program first time on Ubuntu Linux for preparing my college exam. 🙂

    Reply
  4. rashmi

    how to write graphic programming
    what is the replacement of graphics.h

    Reply
  5. Li ZHANG

    A noise-free tutorial I’ve been looking for !
    Absolutely the best !

    Reply

Leave a Reply

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